Link to home
Start Free TrialLog in
Avatar of ZumbaJr
ZumbaJrFlag for Brazil

asked on

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

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.
ASKER CERTIFIED SOLUTION
Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ZumbaJr

ASKER

It works now. I don´t know what was wrong with the class. The question is that now is working. Thank tou.