Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How can i generate mysql_pconnect link identifier inside a class method and use it outside?

Posted on 2011-04-19
2
Medium Priority
?
277 Views
Last Modified: 2012-06-27
Hi experts!

I´m trying to build a class to make mysql connections and store the connect link identifier in a private attribute to be used after, when i call the query (acessing the link through a class method that return it) but it not works fine.

the code:

<?php
class DbMySql {
      private $connectionId; /* stores object connection id */
      function __construct      (      $_hostname,                   // hostname
                                           $_dbName,                   // database name to select
                                           $_username,                   // username
                                           $_password                  // password
      {
            try {
                  $this->connectionId = mysql_pconnect($_hostname, $_username, $_password); // establishes db connection
                  mysql_select_db($_dbName, $this->connectionId); // database select
            } catch (Exception $e) {
                  echo "ERRO: " .$e->getMessage();
                  $this->connectionId = false;
            }
      }

      function id()
      {
            try {
                  return $this->connectionId;
            } catch (Exception $e) {
                  echo "ERRO: " .$e->getMessage();
                  return NULL;
            }
      }
}

$connection = new DbMySql("127.0.0.1", "database1", "user", "password");

$query = "SELECT * FROM TABLE1";
$result = mysql_query($query, $connectin->id()) or die(mysql_error());
$totalRows = mysql_num_rows($result);
echo $totalRows;
?>

Experts, it returns always 0 and there are 50 rows on table.

When i print $connection->id() id returns: Resource id #3, seems ok. If i put the code outside the class, it works fine.

Somebody can help me?

Thanks in advance.
Paulo.
0
Comment
Question by:ZumbaJr
2 Comments
 
LVL 19

Accepted Solution

by:
Greg Alexander earned 1500 total points
ID: 35429553
Try this... I think there is a missing prin as well as the newly created function is being called incorrectly
<?php
class DbMySql {
      private $connectionId; /* stores object connection id */
      function __construct      (      $_hostname,                   // hostname
                                           $_dbName,                   // database name to select
                                           $_username,                   // username
                                           $_password)                  // password
      {
            try {
                  $this->connectionId = mysql_pconnect($_hostname, $_username, $_password); // establishes db connection
                  mysql_select_db($_dbName, $this->connectionId); // database select
            } catch (Exception $e) {
                  echo "ERRO: " .$e->getMessage();
                  $this->connectionId = false;
            }
      }

      function id()
      {
            try {
                  return $this->connectionId;
            } catch (Exception $e) {
                  echo "ERRO: " .$e->getMessage();
                  return NULL;
            }
      }
}

$connection = new DbMySql("127.0.0.1", "database1", "user", "password");

$query = "SELECT * FROM TABLE1";
$result = mysql_query($query, $connection->id()) or die(mysql_error());
$totalRows = mysql_num_rows($result);
echo $totalRows;
?>

Open in new window

0
 

Author Closing Comment

by:ZumbaJr
ID: 35429681
It works now. I don´t know what was wrong with the class. The question is that now is working. Thank tou.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question