szym9341 Posted April 21, 2009 Share Posted April 21, 2009 This is a continuation of another problem that I was having... I have two classes (dbConnector & getTodoCats) and one script. When I use 'or die(mysql_error());' in my query function I get: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /mydirectories/dbConnector.php on line 42 If you have any idea why this is occurring, please help... when I take off the 'mysql_error', the page works fine. The script: <?php ini_set('display_errors', '1'); error_reporting(E_ALL); require_once('../_library/getTodoCats.php'); $si = ''; $si['additional'][0] = " WHERE t1.project_id = '365'"; $data = new getTodoCats($si); $data = $data->getAllData(); foreach ($data as $i) { echo $i['todo_cat'];; } ?> getTodoCats Class: <?php require_once('dbConnector.php'); class getTodoCats { public $sql; function __construct($si = "") { // limit select, if needed if ((isset($si['select']) && ($si['select'] != NULL))) { $select = $si['select']; } else { $select = '*'; } // foundational SQL $this->sql = "SELECT $select FROM todo_cats AS t1 "; // additional SQL, if needed if ($si['additional'] != NULL) { foreach ($si['additional'] as $i) { $this->sql .= " $i"; } } } function getSql() { return $this->sql; } function getNumRows() { $dbConnector = new dbConnector('main'); $query = $dbConnector->query($this->sql); $num = $dbConnector->getNumRows($query); return $num; } function getAllData() { $dbConnector = new dbConnector('main'); $sql = $dbConnector->query($this->sql); $i = 0; $thedata = array(); while ($row = $dbConnector->getArray($sql)) { $thedata[$i]['todo_cat_id'] = $row['todo_cat_id']; $thedata[$i]['todo_cat'] = $row['todo_cat']; $thedata[$i]['project_id'] = $row['project_id']; $i++; } return $thedata; } } ?> the dbConnector class: <?php require_once ('/home/szym9341/includes/systemComponent.php'); class dbConnector extends systemComponent { public $theQuery; public $link; function __construct($which_db) { // Load settings from parent class $settings = systemComponent::getSettings(); if ($which_db == 'main') { // array of zero = main database; add else if for another connection $host = $settings[0]['dbhost']; $db = $settings[0]['dbname']; $user = $settings[0]['dbusername']; $pass = $settings[0]['dbpassword']; } // Connect to the database $this->link = mysql_connect($host, $user, $pass); mysql_select_db($db); } //*** Function: query, Purpose: Execute a database query *** function query($query) { $this->theQuery = $query; // return mysql_query($query, $this->link); return mysql_query($query, $this->link) or die(mysql_error()); } //*** 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 getArray($query) { return mysql_fetch_array($query); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155104-solved-php-inheritance-supplied-argument-is-not-a-valid-mysql/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 21, 2009 Share Posted April 21, 2009 It would appear that you are overwriting your $data object with an array of data. $data is no longer an instance of your class after this point in the code - $data = $data->getAllData(); Quote Link to comment https://forums.phpfreaks.com/topic/155104-solved-php-inheritance-supplied-argument-is-not-a-valid-mysql/#findComment-815910 Share on other sites More sharing options...
szym9341 Posted April 22, 2009 Author Share Posted April 22, 2009 Thanks for the reply. I was desperately hoping this would fix the problem, so I changed the script to the following, but the warning still appears. warning: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /dirs/dbConnector.php on line 42 changed script: $si = ''; $si['additional'][0] = " WHERE t1.project_id = '365'"; $data = new getTodoCats($si); $hmm2 = $data->getAllData(); foreach ($hmm2 as $i) { echo $i['todo_cat'];; } Quote Link to comment https://forums.phpfreaks.com/topic/155104-solved-php-inheritance-supplied-argument-is-not-a-valid-mysql/#findComment-816069 Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2009 Share Posted April 22, 2009 Is the posted code for the query() function current, it still has the or die(...) statement in it? I don't see any obvious problems. Is there any other code that is calling the getAllData() or getArray() functions? Quote Link to comment https://forums.phpfreaks.com/topic/155104-solved-php-inheritance-supplied-argument-is-not-a-valid-mysql/#findComment-816089 Share on other sites More sharing options...
szym9341 Posted April 22, 2009 Author Share Posted April 22, 2009 Thanks again for the reply... I've stripped everything down to bare minimum and still getting the warning flag. Yes, the 'or die()' is still part of the query function. When I take away the 'or die(mysql_error()' the array prints fine... no warnings/errors... Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /dirs/tester.php on line 31 updated in total (tester.php): <?php ini_set('display_errors', '1'); error_reporting(E_ALL); require_once ('/dirs/systemComponent.php'); class dbConnector extends systemComponent { public $link; function __construct() { $settings = systemComponent::getSettings(); $host = $settings[0]['dbhost']; $db = $settings[0]['dbname']; $user = $settings[0]['dbusername']; $pass = $settings[0]['dbpassword']; $this->link = mysql_connect($host, $user, $pass); mysql_select_db($db); } function query($query) { return mysql_query($query, $this->link) or die(mysql_error()); } function getArray($query) { return mysql_fetch_array($query); } } $sql = "SELECT * FROM todo_cats"; $dbConnector = new dbConnector(); $query = $dbConnector->query($sql); $row = $dbConnector->getArray($query); print_r($row); ?> Quote Link to comment https://forums.phpfreaks.com/topic/155104-solved-php-inheritance-supplied-argument-is-not-a-valid-mysql/#findComment-816102 Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2009 Share Posted April 22, 2009 I have seen a problem "returning" the output from a function call (specifically a mysql_query()) and the php manual hints that it does not work (I suspect it depends on what function is called and if it is internally written correctly). Try changing the following - return mysql_query($query, $this->link) or die(mysql_error()); to this - $result = mysql_query($query, $this->link) or die(mysql_error()); return $result; Edit: Actually if you want to test that before you make the modification, use var_dump() on the $query variable - $query = $dbConnector->query($sql); var_dump($query); $row = $dbConnector->getArray($query); I believe you will get a NULL out. Quote Link to comment https://forums.phpfreaks.com/topic/155104-solved-php-inheritance-supplied-argument-is-not-a-valid-mysql/#findComment-816104 Share on other sites More sharing options...
szym9341 Posted April 22, 2009 Author Share Posted April 22, 2009 YESSSSSSSSS! Thank you! Whew, that threw me for a loop. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/155104-solved-php-inheritance-supplied-argument-is-not-a-valid-mysql/#findComment-816110 Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2009 Share Posted April 22, 2009 I'm not sure if this is a bug or just poor documentation. mysql_query() can return a BOOL or a resource depending if the query works or fails and even what kind of query, so it might take assigning the results to a variable to "fix" exactly what type it is so that the return statement can properly use it. Quote Link to comment https://forums.phpfreaks.com/topic/155104-solved-php-inheritance-supplied-argument-is-not-a-valid-mysql/#findComment-816114 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.