synking Posted July 8, 2008 Share Posted July 8, 2008 i am attempting to create a php cms using a tutorial and the tutorial used php4 and this line //***function: FetchArray, Purpose: Get array of query results*** function fetchArray($result){ return mysql_fetch_array($result); } but i get this error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/jking/public_html/includes/DbConnector.php on line 44 when attempting to call the function with this code. <b>what's New: </b><br> <?php // Require the database class require_once('includes/DbConnector.php'); // Create an object (instance) of the Dbconnector $connector = new DbConnector(); //Execure the query to retrive the articles $result = $connector->query('SELCET ID, title FROM cmsarticles ORDER BY ID DESC LIMIT 0,5'); // Get an array containing the results. // Loop for each item in that array while ($row = $connector->fetchArray($result)){ echo '<p><a href="viewArticle.php?id='.$row['ID'].'">'; echo $row['title']; echo '</a> </p>'; } ?> can anyone help with this. Quote Link to comment Share on other sites More sharing options...
jkewlo Posted July 8, 2008 Share Posted July 8, 2008 yeah try $return=mysql_fetch_array($result); Quote Link to comment Share on other sites More sharing options...
synking Posted July 8, 2008 Author Share Posted July 8, 2008 thanks Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 8, 2008 Share Posted July 8, 2008 The error means that your query failed and returned a FALSE value instead of a result resource and your code has no error checking in it so it blindly attempted to execute a mysql_fetch_array() on a FALSE value instead of a result resource. You need to test if a query works before attempting to access any data from the query. Quote Link to comment Share on other sites More sharing options...
synking Posted July 8, 2008 Author Share Posted July 8, 2008 so you mean just add an or die call in the main fetch array. something like this return mysql_fetch_array($result) or die ("Could not access data: " . mysql_error()); or would i add that where i connect Quote Link to comment Share on other sites More sharing options...
synking Posted July 8, 2008 Author Share Posted July 8, 2008 i added the die call after the fetch array and i get this warning message now. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/jking/public_html/includes/DbConnector.php on line 44 could Not fetch data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELCET ID, title FROM cmsarticles ORDER BY ID DESC LIMIT 0,5' at line 1 anyone able to help. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 8, 2008 Share Posted July 8, 2008 SELCET is spelled wrong. Quote Link to comment Share on other sites More sharing options...
synking Posted July 8, 2008 Author Share Posted July 8, 2008 ha ha thanks.... fixed that issue but now i just get can not fetch data from the die message.... no mysql error or php error. that means that the data base is empty correct. Quote Link to comment Share on other sites More sharing options...
synking Posted July 9, 2008 Author Share Posted July 9, 2008 is anyone able to help on this i have been working on it and now i am back to the original error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/jking/public_html/includes/DbConnector.php on line 46 and the line in question function fetchArray($result){ return mysql_fetch_array($result) or die ("could Not fetch data: ". mysql_error()); i have read over the way to use that but i am not sure what i am doing wrong....anyone got anything i could read to find out. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 9, 2008 Share Posted July 9, 2008 mysql_fetch_array() does not set mysql_error(), so your code is not really doing what you think. The or die(...) needs to be on the mysql_query() statement. The reason your code previously output the correct mysql error was - the mysql_query() failed and set up mysql_error() with the reason why. You then called the mysql_fetch_array() and it returned a false value. This caused the or die() statement to output the query error that was already set. For your present problem, either the result resources has been overwritten or the query was a type that does not return a result resource, such as an UPDATE (in which case you should not be performing a mysql_fetch_array() as that only applies to a SELECT query.) You would need to show the query that is being executed and what your current code is that is causing the error. Quote Link to comment Share on other sites More sharing options...
synking Posted July 9, 2008 Author Share Posted July 9, 2008 ok so here is the DbConnector.php <?php ////////////////////////////////////////////////// //Class: DbConnector //Purpose; Connect to 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['dbusername']; $pass = $settings['dbpassword']; //Connect to the database $this->link=mysql_connect($host, $user, $pass) or die("Could not connect: " . mysql_error()); 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) or die ("unable to run query: ". mysql_error()); } //***function: FetchArray, Purpose: Get array of query results*** function fetchArray($result){ return mysql_fetch_array($result) or die ("could Not fetch data: ". mysql_error()); } //***Function: close, Purpose: Close the connection *** function close(){ mysql_close($this->link); } } ?> and the index.php i am having issue with. <html><head><title></title></head> <body> <b>what's New: </b><br> <?php // Require the database class require_once('includes/DbConnector.php'); // Create an object (instance) of the Dbconnector $connector = new DbConnector(); //Execure the query to retrive the articles $result = $connector->query('SELECT ID, title FROM cmsarticles ORDER BY ID DESC LIMIT 0,5') or die ("Could not fetch: " . mysql_error()); // Get an array containing the results. // Loop for each item in that array while ($row = $connector->fetchArray($result)){ echo '<p><a href="viewArticle.php?id='.$row['ID'].'">'; echo $row['title']; echo '</a> </p>'; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 9, 2008 Share Posted July 9, 2008 The current problem is because the return statement in the function code is causing the "or" in the or die() to be evaluated, returning a true value instead of the result resource (I think the "return" section of the php manual might actually state something about this.) Either assign the result to a variable and return the variable or remove the or die() part in the function code. You should just do error checking on one place anyway. Either in the function or in the main code, but not both. Quote Link to comment Share on other sites More sharing options...
synking Posted July 9, 2008 Author Share Posted July 9, 2008 thanks it is kinda working now could you suggest a place to find out more about error handling and checking Quote Link to comment 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.