Iconate Posted May 25, 2009 Share Posted May 25, 2009 Hello, I have a feeling this error may be caused by scope issue, but I continuously get the error "Notice: Undefined variable: result in ... on line 38 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in ... on line 38" I added on the error tags to the mysql_query and I don't get any issues. Note that it is a function too, and I only call it once. I also tried using MYSQL_ASSOC as a parameter for mysql_fetch_array(), but that didn't work at all $result = mysql_query($sql) or die(mysql_error()); function timeCompare() { while ($row = mysql_fetch_array($result)) { echo "<td>". $row["date"] ."</td>"; } } echo "<table border='1'>"; echo "<tr>"; echo "<th>Time</th>"; echo "<th>Availablility</th>"; echo "</tr>"; echo "<tr>"; echo "<td>08:00</td>"; timeCompare(); echo "<tr>"; For now I am just trying to the while loop working before I move on to anything else. I know result cant be wrong, because I had a while loop following my headers before, so the rows were dynamic, now I need the elements in the table to be dynamic, which is why I have the function call. Link to comment https://forums.phpfreaks.com/topic/159538-solved-result-is-not-false-yet-errors-still/ Share on other sites More sharing options...
Andy-H Posted May 25, 2009 Share Posted May 25, 2009 $result = mysql_query($sql) or die(mysql_error()); function timeCompare($res) { while ($row = mysql_fetch_array($res)) { timeCompare($result); Variables cannot be used inside a function scope without being superglobal scope, created within the scope of the function or passed as a function parameter. Link to comment https://forums.phpfreaks.com/topic/159538-solved-result-is-not-false-yet-errors-still/#findComment-841546 Share on other sites More sharing options...
vbnullchar Posted May 25, 2009 Share Posted May 25, 2009 The scope of a variable is the context within which it is defined. use the global keyword to access variables outside the function.. $result = mysql_query($sql) or die(mysql_error()); function timeCompare() { [b]global $result[/b] while ($row = mysql_fetch_array($result)) { echo "<td>". $row["date"] ."</td>"; } } Link to comment https://forums.phpfreaks.com/topic/159538-solved-result-is-not-false-yet-errors-still/#findComment-841547 Share on other sites More sharing options...
trq Posted May 25, 2009 Share Posted May 25, 2009 Besides the scope issue. Just because mysql_query returns true does not meen it returned any records. You should check mysql_num_rows prior to executing mysql_fetch_* Link to comment https://forums.phpfreaks.com/topic/159538-solved-result-is-not-false-yet-errors-still/#findComment-841566 Share on other sites More sharing options...
Iconate Posted May 25, 2009 Author Share Posted May 25, 2009 That fixed everything Thank you very much, you guys have been great help. (also gotta figure out how to mark topics as solved ) EDIT Besides the scope issue. Just because mysql_query returns true does not meen it returned any records. You should check mysql_num_rows prior to executing mysql_fetch_* Yeah, I know this, I am just getting this aspect to work first. I have other global variables that were defined outside the function which I need to use. Link to comment https://forums.phpfreaks.com/topic/159538-solved-result-is-not-false-yet-errors-still/#findComment-841701 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.