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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mySQL Search

Status
Not open for further replies.

billwatson

Instructor
Dec 18, 2002
2,312
CA
hi

dont know much php but have been attempting to alter a downloaded piece of code to perform a search on a mysql database and return the results to flash

<?php
mysql_connect("localhost","bill","pass");
mysql_select_db("products");

$table = "produce";
$condition = $HTTP_POST_VARS['word'];
$qr = mysql_query("SELECT * FROM $table WHERE product LIKE $condition");

$r_string = 'n='.mysql_num_rows ($qr);
$i = 0;
while ($row = mysql_fetch_assoc ($qr)) {
while (list ($key, $val) = each ($row)) {
$r_string .= '&' . $key . $i . '=' . $val ;
}
$i++;
}
echo $r_string;

?>


the code returns the following errors

PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\phpdev\ on line 9
PHP Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in c:\phpdev\ on line 11


any pointers as to what I am doing wrong will be greatly appreciated
 
try adding or die(mysql_error()); after your mysql_query(); it tells you what is wrong with your query!

also try this query:

Code:
$qr = mysql_query("SELECT * FROM $table WHERE product LIKE '%$condition%'");


Regards,

Martin

Gaming Help And Info:
 
thanks martin,

tried your changes but still gives same 2 errors posted above
 
does it say what error it gives after you put or die(mysql_error()); after your query?

if so can you post it here please!


Regards,

Martin

Gaming Help And Info:
 
try assigning a pointer to your database connection:

Code:
$connection=mysql_connect("localhost","bill","pass");
mysql_select_db("products");

and then when you run your query, use the pointer:

Code:
$result=mysql_query("SELECT...",$connection);
$num_rows=mysql_num_rows($result);
etc....

 
Are you sure that "product" is a field of $table? And try "WHERE product[red]=[/red]$condition" just for some variation.

I recently encountered this problem, and the cause was that an invalid SQL line caused my mysql resource to be defined as false, I believe, instead of a resource.

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
thanks all for your help

i found another script that i was able to make work just as i wanted
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top