phpstuck Posted November 20, 2007 Share Posted November 20, 2007 OK I get the following error when running this on my server.... any ideas? "Title: 650 New!! Warning: mysql_fetch_array(): 4 is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\jhc\csidisplay.php on line 6" It returns the tag number and the phrase New!! but develops this error, any help is greatly appreciated! <? include "db.php"; $sql_date = mysql_query("SELECT tag, ro, veh, cust, phone, warr, appt, wait, tech, type, notes, dateadded , ( TO_DAYS(CURDATE()) - TO_DAYS(dateadded) ) as date_diff FROM repairs") or die (mysql_error()); while(list($tag, $ro, $veh, $cust, $phone, $warr, $appt, $wait, $tech, $type, $notes, $dateadded, $date_diff)=mysql_fetch_array($sql_date)){ if($date_diff > 14){ $new = "y"; } echo "Title: $tag"; if($new == "y"){ echo " <font color='red'>New!!</font>"; } mysql_free_result($sql_date); unset($new); } ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 20, 2007 Share Posted November 20, 2007 are you getting an sql error on line 4? Quote Link to comment Share on other sites More sharing options...
phpstuck Posted November 20, 2007 Author Share Posted November 20, 2007 Nope...no error on line 4... It prints the desired result, but then shows that warning right after it... The code and result are exact and full that I posted... Makes no sense. It appears the error is on line 6, but it all seems right to me. Quote Link to comment Share on other sites More sharing options...
phpstuck Posted November 20, 2007 Author Share Posted November 20, 2007 Oh well I will just make it invisible for now using the trusty ole' <? include "db.php"; error_reporting(~E_ALL); $sql_date = mysql_query.... But I still sure would like to know what is causing the warning to pop up there... If anyone knows I am watching for the answer so that I can learn from my mistake. Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 20, 2007 Share Posted November 20, 2007 Always dump the variables... you'll learn to troubleshoot your own scripts quickly. Run this script and post the results: <? include "db.php"; $sql_date = mysql_query("SELECT tag, ro, veh, cust, phone, warr, appt, wait, tech, type, notes, dateadded , ( TO_DAYS(CURDATE()) - TO_DAYS(dateadded) ) as date_diff FROM repairs") or die (mysql_error()); $row = mysql_fetch_array($sql_date); echo"$sql_date<br /><pre>"; print_r($row); die("</pre>"); while(list($tag, $ro, $veh, $cust, $phone, $warr, $appt, $wait, $tech, $type, $notes, $dateadded, $date_diff)=mysql_fetch_array($sql_date)){ if($date_diff > 14){ $new = "y"; } echo "Title: $tag"; if($new == "y"){ echo " <font color='red'>New!!</font>"; } mysql_free_result($sql_date); unset($new); } ?> PhREEEk Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 20, 2007 Share Posted November 20, 2007 mysql_free_result($sql_date);<--- that i guess is the prob you free the result inside the loop so when the loop run for the second time no more resource data to be used Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 20, 2007 Share Posted November 20, 2007 mysql_free_result($sql_date);<--- that i guess is the prob you free the result inside the loop so when the loop run for the second time no more resource data to be used Nice catch.. yes, proper code formatting would have made that so much more apparent. Also, to the OP, you don't need to free the result set unless you really need to, which is rather rare. So, 2 solutions: <?php include "db.php"; $sql_date = mysql_query("SELECT tag, ro, veh, cust, phone, warr, appt, wait, tech, type, notes, dateadded , ( TO_DAYS(CURDATE()) - TO_DAYS(dateadded) ) as date_diff FROM repairs") or die (mysql_error()); while(list($tag, $ro, $veh, $cust, $phone, $warr, $appt, $wait, $tech, $type, $notes, $dateadded, $date_diff)=mysql_fetch_array($sql_date)) { if($date_diff > 14) { $new = "y"; } echo "Title: $tag"; if($new == "y") { echo " <font color='red'>New!!</font>"; } unset($new); } mysql_free_result($sql_date); ?> Or just <?php include "db.php"; $sql_date = mysql_query("SELECT tag, ro, veh, cust, phone, warr, appt, wait, tech, type, notes, dateadded , ( TO_DAYS(CURDATE()) - TO_DAYS(dateadded) ) as date_diff FROM repairs") or die (mysql_error()); while(list($tag, $ro, $veh, $cust, $phone, $warr, $appt, $wait, $tech, $type, $notes, $dateadded, $date_diff)=mysql_fetch_array($sql_date)) { if($date_diff > 14) { $new = "y"; } echo "Title: $tag"; if($new == "y") { echo " <font color='red'>New!!</font>"; } unset($new); } ?> PhREEEk Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 20, 2007 Share Posted November 20, 2007 note never ever free the result inside the loop!!! Quote Link to comment Share on other sites More sharing options...
phpstuck Posted November 20, 2007 Author Share Posted November 20, 2007 Thanks everyone... the seemingly impossible is not so scary...LOL 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.