mickeeyone Posted May 22, 2012 Share Posted May 22, 2012 I have code retrieving a max id column in mysql and the code performs and works correctly, however I receive this error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in ... Here is the code: $query = "SELECT MAX(`pageId`) AS `pageId` FROM `pages` WHERE 1"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $pageId = $row['pageId'] + 1; $query = "INSERT INTO `pages` VALUES('', '$pageId', '$pageName', '$pageTitle', '$pageBody', now(), now(), '$pageAuthor', '$authorIPCreated', '$authorIPCreated')"; $result = mysql_query($query) or die('Error: ' . mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/262901-php-exposes-mysql-error-mysql-exposes-no-error/ Share on other sites More sharing options...
kicken Posted May 22, 2012 Share Posted May 22, 2012 $query = "SELECT MAX(`pageId`) AS `pageId` FROM `pages` WHERE 1"; If you don't have a condition to apply to a query, then just omit the WHERE clause entirely. $query = "SELECT MAX(`pageId`) AS `pageId` FROM `pages`"; Change your first mysql_query line to also include an or die with the mysql error message so you can see what mysql is complaining about: $query = "SELECT MAX(`pageId`) AS `pageId` FROM `pages`"; $result = mysql_query($query) or die('Error: ' . mysql_error()); while($row = mysql_fetch_assoc($result)) Also selecting the max page id and doing +1 and using for a new id is a poor method, you should use mysql's AUTO_INCREMENT feature instead to let mysql automatically generate the IDs. That way you do not have to worry about possible duplicates from simultaneous requests or re-used IDs from a delete. Quote Link to comment https://forums.phpfreaks.com/topic/262901-php-exposes-mysql-error-mysql-exposes-no-error/#findComment-1347496 Share on other sites More sharing options...
mickeeyone Posted May 22, 2012 Author Share Posted May 22, 2012 Thanks for the advice worked great! Quote Link to comment https://forums.phpfreaks.com/topic/262901-php-exposes-mysql-error-mysql-exposes-no-error/#findComment-1347498 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.