I am trying to do a simple MySQL query using my db class, but for some reason I am having issues with getting the query through using my db class. If I hard code the db call bypassing the class, it works fine. Here is the class:
<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: DbConnector
// Purpose: Connect to a database, MySQL version
///////////////////////////////////////////////////////////////////////////////////////
require_once('systemcomponent.php');
class dbconnector extends systemComponent {
var $theQuery;
var $link;
//*** Function: DbConnector, Purpose: Connect to the database ***
function dbConnector(){
// Load settings from parent class
$settings = systemComponent::getSettings();
// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$db = $settings['dbname'];
$user = $settings['dbuser'];
$pass = $settings['dbpassword'];
// Connect to the database
$this->link = mysql_connect($host, $user, $pass);
mysql_select_db($db);
register_shutdown_function(array(&$this, 'close'));
}
//*** Function: query, Purpose: Execute a database query ***
function query($query) {
$this->theQuery = $query;
return mysql_query($query, $this->link);
}
//*** Function: getQuery, Purpose: Returns the last database query, for debugging ***
function getQuery() {
return $this->theQuery;
}
//*** Function: getNumRows, Purpose: Return row count, MySQL version ***
function getNumRows($result){
return mysql_num_rows($result);
}
//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {
return mysql_fetch_array($result);
}
//*** Function: close, Purpose: Close the connection ***
function close() {
mysql_close($this->link);
}
}
?>
systemComponent.php simply feeds the class the db login, password, user, etc info.
Here is how I am calling it.
$db = new dbConnector();
$getAll = $db->query("SELECT * from `cmsnews`");
$rows = $db->getNumRows($getAll);
echo $rows;
echo $db->getQuery();
The class correctly does the getQuery() call and it shows the current query, however I get the
"Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\webs\etalenttraks\includes\dbconnector.php on line 45"
from doing the getNumRows method. Am I missing something here?
Thanks!