Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting error processing search

Status
Not open for further replies.

dunskii

Programmer
Sep 14, 2001
107
AU
Hi all,
I'm trying to create a search page, but when the search is processed i keep getting the following error:

Warning: Supplied argument is not a valid MySQL result resource

heres the code

<?php

$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;);


$query = &quot;SELECT * FROM Barrister.Barristers WHERE Barrister.Admissions LIKE '%$bar_search%' OR Barrister.Baristers LIKE '%$bar_search%'&quot;;

echo &quot;Search results:<br><br>&quot;;

$result = mysql_db_query(&quot;fjc&quot;, $query);
while($r = mysql_fetch_array($result));
{
$bar_search = $r[&quot;bar_search&quot;];

echo &quot;$bar_search&quot;;
}


?>

Being new to php i think it is something simple that i cant yet see.

Thanks again...

Dunskii :)
 
while($r = mysql_fetch_array($result));
{
$bar_search = $r[&quot;bar_search&quot;];

echo &quot;$bar_search&quot;;
}
----------------------------
something like this may prove to give better results:

$result = mysql_query(&quot;SELECT * FROM Barrister.Barristers WHERE Barrister.Admissions LIKE '%$bar_search%' OR Barrister.Baristers LIKE '%$bar_search%'&quot;,$db);

while ($myrow = mysql_fetch_array($result)) {
printf(&quot;%&quot;,$myrow[0]);
} ***************************************
Party on, dudes!
 
ooops (doh hangover) yeah , what BB101

/runs off to hide ***************************************
Party on, dudes!
 
Ok i just tries that, but i'm still getting the same error. Warning: Supplied argument is not a valid MySQL result resource.

It reckons that the problem is with the while statement

while ($myrow = mysql_fetch_array($result)) {

Thanks,

Andrew
 
Dunskii - a couple of points here:

1. mysql_select_db() is meant to be used with mysql_query(), not with mysql_db_query(). mysql_db_query is the old (deprecated) method. It should still work, but it will be gone soon. See and
2. You have a semicolon after while($r = mysql_fetch_array($result)), even though it is the opening of a while loop. When you are opening a while loop, you should only follow it by an opening brace({), which you did properly on the next line.

3. You are trying to connect as root without a password. Have you defined a default password in php.ini, or do you have no password for root?

4. Finally, while developing, try enabling some debugging output:
Code:
<?php

$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;)or die(mysql_error() . &quot; at line &quot; . __LINE__ . &quot; in file &quot; . __FILE__);


     $query = &quot;SELECT * FROM Barrister.Barristers WHERE Barrister.Admissions LIKE '%$bar_search%' OR Barrister.Baristers LIKE '%$bar_search%'&quot;;

     echo &quot;Search results:<br><br>&quot;;
     mysql_select_db(&quot;fjc&quot;, $db);
     $result = mysql_query($query)or die(mysql_error() . &quot; at line &quot; . __LINE__ . &quot; in file &quot; . __FILE__);

    $i = 0;

    while($r = mysql_fetch_array($result))
        {
        $bar_search = $r[&quot;bar_search&quot;];
         echo &quot;$bar_search&quot;;
         $i++;
        }
    if(!$i)
    {
    print (mysql_error() . &quot; at line &quot; . __LINE__ . &quot; in file &quot; . __FILE__);
    }
?>

Basically, the above will output the latest MySQL error message whenever any of the operations involving MySQL fail. The last one needs a little explanation: we set a counter calld $i before we start the while loop. If we never even go through one iteration of the loop, the counter is still zero, which evaluates to false, so the final MySQL error would be printed underneath.

Try this, and see if it helps you pinpoint the problem. -------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
                          -unknown F student
 
Thanks for the debugging tip rycamor,

It now says that it cant see the table.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top