We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

ZumbaJr
ZumbaJr asked
on
Medium Priority
308 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.
Comment
Watch Question

Lead Developer
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
It works now. I don´t know what was wrong with the class. The question is that now is working. Thank tou.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.