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!

why does this function not work?

Status
Not open for further replies.

wreikun

Technical User
Apr 23, 2002
63
US
hey everyone. im in the process of learning MySQL with PHP from a book, and one of the example scripts doesnt seem to be working. The problem lies in the built-in function 'mysql_num_rows()'. part of the example script is listed below with the 'mysql_num_row()' function. Can anyone let me know what I'm getting wrong here? when the php script is run, i get the following displayed in my browser:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

this is an excerpt of the php script:

mysql_select_db("books");
$query = "select * from books where ".$searchtype." like '%".searchterm."%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);

echo &quot;<p>Number of books found: &quot;.$num_results.&quot;</p>&quot;;

for ($i=0; $i<$num_results; $i++){
$row = mysql_fetch_array($result);
echo &quot;<p><strong>&quot;.($i+1).&quot;. Title: &quot;;
echo htmlspecialchars(stripslashes($row[&quot;title&quot;]));
echo &quot;</strong><br>Author: &quot;;
echo htmlspecialchars(stripslashes($row[&quot;author&quot;]));
echo &quot;<br>ISBN: &quot;;
echo htmlspecialchars(stripslashes($row[&quot;isbn&quot;]));
echo &quot;<br>Price: &quot;;
echo htmlspecialchars(stripslashes($row[&quot;price&quot;]));
echo &quot;</p>&quot;;
}

thanks,
REI

 
Try changing
Code:
$query = &quot;select * from books where &quot;.$searchtype.&quot; like '%&quot;.searchterm.&quot;%'&quot;;
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
to
Code:
$query = &quot;select * from books where &quot;.$searchtype.&quot; like '%&quot;.$searchterm.&quot;%'&quot;;
$result = mysql_query($query) or die(&quot;Couldn't query&quot;);
$num_results = mysql_num_rows($result);
This would print an error if the querying doesn't work. //Daniel
 
Hello. Thanks for the tip on how to check if the querying works or not. I added the 'die(&quot;Couldnt query&quot;)' and tried viewing the php page through my browser and the words &quot;Couldnt query&quot; were displayed which obviously means that the querying doesnt work.

Now my question is: what are some possible reasons on why the mysql_query() function isnt working?

Any help is appreciated!

Thanks in advance,
REI
 
Try printing the contents of the query. Does it look right? Could you post it here?
mysql_query returns false if it fails, meaning that the query returned an error from the database backend. //Daniel
 
Thanks for the reply. Ive been trying to figure this out for a lil while now, and Ive been able to narrow it down to this line:

mysql_connect(&quot;localhost&quot;);

I wasnt able to connect to mysql server, thus the functions mysql_query(), and mysql_num_rows() didnt work.

I changed that line to the following:
mysql_connect(&quot;localhost&quot;, &quot;root&quot;);

and it worked as expected.

Now, I gotta try to figure out how this whole username thing works in mysql.

thanks for the help daniel.

--REI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top