ZumbaJr
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.
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,
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER