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.
ZumbaJrAsked:
Who is Participating?
 
Greg AlexanderLead DeveloperCommented:
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
 
ZumbaJrAuthor Commented:
It works now. I don´t know what was wrong with the class. The question is that now is working. Thank tou.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.