I have a very simple mySqlQuery object that takes two parameters:
1) the string query
2) the db connection resource
I tested and was certain everything is passing correctly (the string
query and the db connection resource). The first mySqlQuery object
produces just fine; but when I run it, the second mySqlQuery object (a
totally different SQL query, same db connection resource) comes back
null every time, even when there is data produced by the second query.
Here is my mySqlQuery class code:
class dbConnection {
var $dbHost,$dbPort ,$dbUser,$dbPwd ,$dbName;
function dbconnection($d bHost,$dbPort,$ dbUser,$dbPwd,$ dbName) {
$this->dbHost = $dbHost;
$this->dbPort = $dbPort;
$this->dbUser = $dbUser;
$this->dbPwd = $dbPwd;
$this->dbName = $dbName;
$this->dbServer = $dbHost . ":" . $dbPort;
}
function connect() {
$dbcnx = @mysql_connect( $this->dbServer, $this->dbUser,
$this->dbPwd);
if (!$dbcnx) {
header("Locatio n: error.php?msg=" . urlencode('Cann ot connect to
the database'));
return false;
} else {
$dbselect = mysql_select_db ($this->dbName);
}
if (!$dbselect) {
header("Locatio n: error.php?msg=" . urlencode('Cann ot select the
database'));
return false;
} else {
return $dbcnx;
}
}
function close() {
$closed = mysql_close($th is->connect());
if (!$closed) {
header("Locatio n: error.php?msg=" . urlencode('Cann ot close the
connection to the database'));
return false;
}
return $closed;
}
}
class mySqlQuery {
var $query,$dbcnx; // $dbcnx is a mySqlConnection object
function mySqlQuery($que ry,$dbcnx) {
$this->query = $query;
$this->dbcnx = $dbcnx;
}
function runQuery() {
global $devpath,$DOCUM ENT_ROOT;
$result = mysql_query($th is->query, $this->dbcnx) or
die(@include("$ DOCUMENT_ROOT${ devpath}errors/query_error.php "));
return $result;
}
function getResult() {
$runquery = $this->runQuery();
$count = 0;
while ($row = mysql_fetch_obj ect($runquery)) {
$result[$count] = $row;
$count++;
}
return $result;
}
function getRows($what = 'select') {
$runquery = $this->runQuery();
$count = 0;
if (strcmp(strtolo wer($what), 'affected') == 0) {
$row = @mysql_affected _rows($this->dbcnx);
} else {
$row = @mysql_num_rows ($this->dbcnx);
}
if (!$row) {
return false;
}
return $row;
}
function freeResult() {
@mysql_free_res ult($this->runQuery());
}
}
Here are the calls to the class object, having to do it twice because
of the logic and nature of the calls:
$db = new dbConnection($d bHost,$dbPort,$ dbUser,$dbPwd,$ dbName);
$dbconnection = $db->connect();
// Get student completion information
$query = "SELECT * FROM intern_complete _application WHERE intern_id
= '" . $this->id . "'";
$queryInfo2 = new mySqlQuery($que ry,$dbconnectio n);
print_r($queryI nfo2);
$result2 = $queryInfo2->getResult();
$objArray = (get_object_var s($result2[0])) ?
get_object_vars ($result2[0]) : array();
if (sizeof($objArr ay) > 0) {
foreach (array_flip($ob jArray) as $key) {
$arrayKeyName = $this->dbNameToArrayN ame($key);
$this->$arrayKeyNam e = $result2[0]->$key;
}
$queryInfo2 = null;
}
print_r("<P>com pletion object<p>");
print_r($result 2[0]);
$result2 = null;
// Get basic information
$query = "SELECT * FROM interns WHERE id='" . $this->id . "'";
$queryInfo = new mySqlQuery($que ry,$dbconnectio n);
print_r($queryI nfo);
$result = $queryInfo->getResult();
$db->close();
print_r("<P>int ern object<P>");
print_r(get_obj ect_vars($resul t[0]));
print_r($result[0]);
When I run the print_r statement I see my "completion object" but I
see no "intern object"; it's null. If I comment out the entire code
block that creates the completion object, THEN the intern object shows
up in full.
I am not sure at this point of a good workaround for this, is this
normal behavior based on what you see so far?
Phil
1) the string query
2) the db connection resource
I tested and was certain everything is passing correctly (the string
query and the db connection resource). The first mySqlQuery object
produces just fine; but when I run it, the second mySqlQuery object (a
totally different SQL query, same db connection resource) comes back
null every time, even when there is data produced by the second query.
Here is my mySqlQuery class code:
class dbConnection {
var $dbHost,$dbPort ,$dbUser,$dbPwd ,$dbName;
function dbconnection($d bHost,$dbPort,$ dbUser,$dbPwd,$ dbName) {
$this->dbHost = $dbHost;
$this->dbPort = $dbPort;
$this->dbUser = $dbUser;
$this->dbPwd = $dbPwd;
$this->dbName = $dbName;
$this->dbServer = $dbHost . ":" . $dbPort;
}
function connect() {
$dbcnx = @mysql_connect( $this->dbServer, $this->dbUser,
$this->dbPwd);
if (!$dbcnx) {
header("Locatio n: error.php?msg=" . urlencode('Cann ot connect to
the database'));
return false;
} else {
$dbselect = mysql_select_db ($this->dbName);
}
if (!$dbselect) {
header("Locatio n: error.php?msg=" . urlencode('Cann ot select the
database'));
return false;
} else {
return $dbcnx;
}
}
function close() {
$closed = mysql_close($th is->connect());
if (!$closed) {
header("Locatio n: error.php?msg=" . urlencode('Cann ot close the
connection to the database'));
return false;
}
return $closed;
}
}
class mySqlQuery {
var $query,$dbcnx; // $dbcnx is a mySqlConnection object
function mySqlQuery($que ry,$dbcnx) {
$this->query = $query;
$this->dbcnx = $dbcnx;
}
function runQuery() {
global $devpath,$DOCUM ENT_ROOT;
$result = mysql_query($th is->query, $this->dbcnx) or
die(@include("$ DOCUMENT_ROOT${ devpath}errors/query_error.php "));
return $result;
}
function getResult() {
$runquery = $this->runQuery();
$count = 0;
while ($row = mysql_fetch_obj ect($runquery)) {
$result[$count] = $row;
$count++;
}
return $result;
}
function getRows($what = 'select') {
$runquery = $this->runQuery();
$count = 0;
if (strcmp(strtolo wer($what), 'affected') == 0) {
$row = @mysql_affected _rows($this->dbcnx);
} else {
$row = @mysql_num_rows ($this->dbcnx);
}
if (!$row) {
return false;
}
return $row;
}
function freeResult() {
@mysql_free_res ult($this->runQuery());
}
}
Here are the calls to the class object, having to do it twice because
of the logic and nature of the calls:
$db = new dbConnection($d bHost,$dbPort,$ dbUser,$dbPwd,$ dbName);
$dbconnection = $db->connect();
// Get student completion information
$query = "SELECT * FROM intern_complete _application WHERE intern_id
= '" . $this->id . "'";
$queryInfo2 = new mySqlQuery($que ry,$dbconnectio n);
print_r($queryI nfo2);
$result2 = $queryInfo2->getResult();
$objArray = (get_object_var s($result2[0])) ?
get_object_vars ($result2[0]) : array();
if (sizeof($objArr ay) > 0) {
foreach (array_flip($ob jArray) as $key) {
$arrayKeyName = $this->dbNameToArrayN ame($key);
$this->$arrayKeyNam e = $result2[0]->$key;
}
$queryInfo2 = null;
}
print_r("<P>com pletion object<p>");
print_r($result 2[0]);
$result2 = null;
// Get basic information
$query = "SELECT * FROM interns WHERE id='" . $this->id . "'";
$queryInfo = new mySqlQuery($que ry,$dbconnectio n);
print_r($queryI nfo);
$result = $queryInfo->getResult();
$db->close();
print_r("<P>int ern object<P>");
print_r(get_obj ect_vars($resul t[0]));
print_r($result[0]);
When I run the print_r statement I see my "completion object" but I
see no "intern object"; it's null. If I comment out the entire code
block that creates the completion object, THEN the intern object shows
up in full.
I am not sure at this point of a good workaround for this, is this
normal behavior based on what you see so far?
Phil