Jump to content

[SOLVED] $result is not FALSE, yet errors still


Iconate

Recommended Posts

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.

 

$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.

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>";
    }
   
}

That fixed everything :D

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.