Jump to content

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result reso


stubarny

Recommended Posts

Hello,

 

I'm getting the following error message on the commented line of code below. 2 results are being returned by the sql command ($nrows = 2). The strange thing is that the error message only triggers on the second result ($i=1).

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in... (i've removed the file address)

 

<?php
$haystack = $_GET[q];
$query ="SELECT * FROM research_job_searches";
$result=mysql_query($query)
  or die ("Connection to database table failed. 35. "  . mysql_error());	
$nrows=mysql_num_rows($result);

if ($nrows<>'0')
{
	for ($i=0;$i<$nrows;$i++)
		{
			# error occurs on line below for $i=1
                                                                $record = mysql_fetch_array($result, MYSQL_BOTH);			
                                                                $needle = $record[research_job_search_url_keyword] . "-jobs";
                                                }
                }
?>

 

Any ideas?

 

Thanks,

 

Stu

just re-created this from different set of source code and it seems to work. I'm not sure what i've done differently but here's the solution:

 

$query ="SELECT * FROM research_job_searches";

$result=mysql_query($query)

or die ("Connection to database table failed. 106."  . mysql_error() );

$nrows=mysql_num_rows($result);

 

if ($nrows<>'0')

{

for ($i=0;$i<$nrows;$i++)

{

$record = mysql_fetch_array($result, MYSQL_ASSOC);

}

}

Try this:

 

<?php
$haystack = $_GET[q];
$query ="SELECT * FROM research_job_searches";
$result=mysql_query($query)
  or die ("Connection to database table failed. 35. "  . mysql_error());	
$nrows=mysql_num_rows($result);

if ($nrows > 0){
while($record = mysql_fetch_array($result, MYSQL_BOTH)){
	$needle = $record[research_job_search_url_keyword] . "-jobs";
	echo $needle."<br>";
}
}
?>

when looping through a list of MySQL results,  you should use a while loop. mysql_fetch_array will return a value every time, and if it doesn't return a value it will return false which will break out of the while loop.

Thanks little Guy,

 

I think I've got to the bottom of it. I was putting the following preg_match within the loop. The outputted $result variable from the preg_match was messing up the msql_fetch_array - can't believe that took me 2 days to figure out!

 

Thanks for your help though - I'll use the while loop in future, it reads more cleanly.

 

Thanks,

 

Stu

 

if (preg_match("/$needle/i", $haystack, $result))

{

    # echo "<br>A match was found.<br>";

# echo "<br>0, $result[0]<br>";

# echo "<br>1, $result[1]<br>";

# echo "<br>2, $result[2]<br>";

 

$display_research_job_search_url_keyword = $research_job_search_url_keyword;

$display_research_job_search_index = $research_job_search_index;

}

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.