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($dbHost,$dbPort,$dbUser,$dbPwd,$dbNam e) {
$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("Location: error.php?msg=" . urlencode('Cannot connect to
the database'));
return false;
} else {
$dbselect = mysql_select_db($this->dbName);
}
if (!$dbselect) {
header("Location: error.php?msg=" . urlencode('Cannot select the
database'));
return false;
} else {
return $dbcnx;
}
}
function close() {
$closed = mysql_close($this->connect());
if (!$closed) {
header("Location: error.php?msg=" . urlencode('Cannot close the
connection to the database'));
return false;
}
return $closed;
}
}
class mySqlQuery {
var $query,$dbcnx; // $dbcnx is a mySqlConnection object
function mySqlQuery($query,$dbcnx) {
$this->query = $query;
$this->dbcnx = $dbcnx;
}
function runQuery() {
global $devpath,$DOCUMENT_ROOT;
$result = mysql_query($this->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_object($runquery)) {
$result[$count] = $row;
$count++;
}
return $result;
}
function getRows($what = 'select') {
$runquery = $this->runQuery();
$count = 0;
if (strcmp(strtolower($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_result($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($dbHost,$dbPort,$dbUser,$dbPwd,$dbNam e);
$dbconnection = $db->connect();
// Get student completion information
$query = "SELECT * FROM intern_complete_application WHERE intern_id
= '" . $this->id . "'";
$queryInfo2 = new mySqlQuery($query,$dbconnection);
print_r($queryInfo2);
$result2 = $queryInfo2->getResult();
$objArray = (get_object_vars($result2[0])) ?
get_object_vars($result2[0]) : array();
if (sizeof($objArray) > 0) {
foreach (array_flip($objArray) as $key) {
$arrayKeyName = $this->dbNameToArrayName($key);
$this->$arrayKeyName = $result2[0]->$key;
}
$queryInfo2 = null;
}
print_r("<P>completion object<p>");
print_r($result2[0]);
$result2 = null;
// Get basic information
$query = "SELECT * FROM interns WHERE id='" . $this->id . "'";
$queryInfo = new mySqlQuery($query,$dbconnection);
print_r($queryInfo);
$result = $queryInfo->getResult();
$db->close();
print_r("<P>intern object<P>");
print_r(get_object_vars($result[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