Zepo. Posted August 5, 2009 Share Posted August 5, 2009 Ok this one's really odd. Even though i can run the query in phpmyadmin without errors when i try to call it using mysql_fetch_array it gives the error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/hogen/public_html/development/v1/index.php on line 78 Also yes, mysql is working on other queries on the page, just this one doesn't want to work. I've tried the or die(mysql_error()) with no error revealed. Here's the code: //Global Functions require("./includes/init.php"); //Lets Loop! $news = mysql_query(" SELECT id, category, title, article, userid, dateposted FROM newsposts WHERE category = 1 ORDER BY id "); while ($news = mysql_fetch_array($news, MYSQL_ASSOC)) { $tpl->assign("loop", array(array("title" => "{$news['title']}", "content" => "{$news['article']}", "userid" => "{$news['userid']}", "userid" => "{$news['date']}"))); } Help, please Quote Link to comment https://forums.phpfreaks.com/topic/168915-solved-mysql_fetch_array-giving-error/ Share on other sites More sharing options...
trq Posted August 5, 2009 Share Posted August 5, 2009 Your code is terrible. Always check your results before trying to use them. eg; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { // it is now safe to call mysql_fetch_* } else { // no results where found } } else { // your query failed } Quote Link to comment https://forums.phpfreaks.com/topic/168915-solved-mysql_fetch_array-giving-error/#findComment-891202 Share on other sites More sharing options...
Zepo. Posted August 5, 2009 Author Share Posted August 5, 2009 Thanks..lol. It worked fine through that. Edit, actually no it doesn't it failed. Hmmm if it fails here, why would it work in phpmyadmin direct copy/paste 0.o Quote Link to comment https://forums.phpfreaks.com/topic/168915-solved-mysql_fetch_array-giving-error/#findComment-891217 Share on other sites More sharing options...
bundyxc Posted August 5, 2009 Share Posted August 5, 2009 Try this: if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { echo 'Everything works.'; echo '<br />'; echo $sql; } else { echo 'No results were found.'; echo '<br />'; echo mysql_error() . " : " . $sql; } } else { echo 'MySQL query failed.'; echo '<br />'; echo mysql_error() . " : " . $sql; } Quote Link to comment https://forums.phpfreaks.com/topic/168915-solved-mysql_fetch_array-giving-error/#findComment-891248 Share on other sites More sharing options...
trq Posted August 5, 2009 Share Posted August 5, 2009 That logic is problematic. mysql_error() returns the last error for your connection, if your request succeeds but produces no results calling mysql_error() will give you false data. Quote Link to comment https://forums.phpfreaks.com/topic/168915-solved-mysql_fetch_array-giving-error/#findComment-891252 Share on other sites More sharing options...
bundyxc Posted August 5, 2009 Share Posted August 5, 2009 So would this be more logical? if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { echo 'Everything works.'; echo '<br />'; echo $sql; } else { echo 'No results were found.'; echo '<br />'; echo $sql; } } else { echo 'MySQL query failed.'; echo '<br />'; echo mysql_error() . " : " . $sql; } Quote Link to comment https://forums.phpfreaks.com/topic/168915-solved-mysql_fetch_array-giving-error/#findComment-891260 Share on other sites More sharing options...
trq Posted August 5, 2009 Share Posted August 5, 2009 Yeah, except I still see no reason to echo the $sql variable. You really shouldn't be echoing a mysql_error() either within a production environment. Your better off triggering an error (passing it the output of mysql_error()) and then catching that later. this way you can handle error gracefully (and log them) within production, or display them easily during development. Quote Link to comment https://forums.phpfreaks.com/topic/168915-solved-mysql_fetch_array-giving-error/#findComment-891264 Share on other sites More sharing options...
Zepo. Posted August 5, 2009 Author Share Posted August 5, 2009 Hmmmm MySQL query failed. 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 'Resource id #10' at line 1 : From what i found on that error it seems to be because the query is called by mysql_query and not fetch. Quote Link to comment https://forums.phpfreaks.com/topic/168915-solved-mysql_fetch_array-giving-error/#findComment-891667 Share on other sites More sharing options...
mikesta707 Posted August 5, 2009 Share Posted August 5, 2009 try not using the $news variable as the variable used to store the assoc array. maybe while ($row = mysql_fetch_array($news, MYSQL_ASSOC)) Quote Link to comment https://forums.phpfreaks.com/topic/168915-solved-mysql_fetch_array-giving-error/#findComment-891675 Share on other sites More sharing options...
Zepo. Posted August 5, 2009 Author Share Posted August 5, 2009 Absolute genius. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/168915-solved-mysql_fetch_array-giving-error/#findComment-891683 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.