stubarny Posted December 11, 2011 Share Posted December 11, 2011 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 Quote Link to comment Share on other sites More sharing options...
stubarny Posted December 11, 2011 Author Share Posted December 11, 2011 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); } } Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 11, 2011 Share Posted December 11, 2011 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>"; } } ?> Quote Link to comment Share on other sites More sharing options...
stubarny Posted December 12, 2011 Author Share Posted December 12, 2011 Hi Little Guy, Well I'm confused, the solution in the 2nd post I put up has stopped working. Then I tried yours and it's working perfectly. Please could you tell me what I did wrong? Thanks, Stu Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 12, 2011 Share Posted December 12, 2011 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. Quote Link to comment Share on other sites More sharing options...
stubarny Posted December 12, 2011 Author Share Posted December 12, 2011 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; } 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.