By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

PHP Help From Book

P: n/a
Does anyone know what's wrong with this code? I just started learning php &
mysql from a book and this code is copied directly from it, yet it still
fails to work... I always get the following error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result
resource in /home/tjc/public_html/results.php on line 40
<?php
// create short varibles names
$searchtype = $HTTP_POST_VARS['searchtype'] ;
$searchterm = $HTTP_POST_VARS['searchterm'] ;

$searchterm = trim($searchterm) ;

if (!$searchtype || !$searchterm)
{
echo "No search details entered" ;
exit ;
}

$searchtype = addslashes($searchtype) ;
$searchterm = addslashes($searchterm) ;

@ $db = mysql_pconnect('localhost', 'tjc_bookorama', 'bookorama123') ;

if (!$db)
{
echo "Error: Could not connect to db" ;
exit ;
}

mysql_select_db ("tjc_books");

$query = "SELECT * FROM books WHERE ".$searchtype." LIKE
'%".$searchterm."%'" ;
$result = mysql_query($query) ;

$num_results = mysql_num_rows($result); // line 40

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

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

echo stripslashes($row['isbn']) ;
echo '<br />Price: ' ;
echo stripslashes($row['price']) ;
echo '</p>' ;
}

?>

************************************************** **************************
*****

QOTSA: "Nicotine, Valium, Vicodin, Marijuana, Ecstacy and Alcohol"
Einstein: "Imagination is More Important Than Knowledge"
Jul 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> Does anyone know what's wrong with this code? I just started learning php
&
mysql from a book and this code is copied directly from it, yet it still
fails to work... I always get the following error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result
resource in /home/tjc/public_html/results.php on line 40
<?php
// create short varibles names
$searchtype = $HTTP_POST_VARS['searchtype'] ;
$searchterm = $HTTP_POST_VARS['searchterm'] ;

$searchterm = trim($searchterm) ;

if (!$searchtype || !$searchterm)
{
echo "No search details entered" ;
exit ;
}

$searchtype = addslashes($searchtype) ;
$searchterm = addslashes($searchterm) ;

@ $db = mysql_pconnect('localhost', 'tjc_bookorama', 'bookorama123') ;

if (!$db)
{
echo "Error: Could not connect to db" ;
exit ;
}

mysql_select_db ("tjc_books");

$query = "SELECT * FROM books WHERE ".$searchtype." LIKE
'%".$searchterm."%'" ;
$result = mysql_query($query) ;

$num_results = mysql_num_rows($result); // line 40

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

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

echo stripslashes($row['isbn']) ;
echo '<br />Price: ' ;
echo stripslashes($row['price']) ;
echo '</p>' ;
}

?>


First off, as a beginner, you should not be suppressing MySQL errors. The @
symbol in front of :
@ $db = mysql_pconnect('localhost', ...'); will suppress error
messages.

Use a function to return any specific MySQL errors:

For instance:

function the_error ($message) {
$mysql_error = mysql_errno() . ": " . mysql_error();
$page = getenv("REQUEST_URI");
$server = getenv("SERVER_NAME");
echo "<h1>Sorry, an error has occured!</h1>\n<P>$message\n\n";
echo "<P><b>MySQL Error Info</b><br>$mysql_error\n";

echo "<P><A HREF=\"mailto:yo*******@youremail.com?subject=Erro r on
http://$server$page&body=Error Message: $message (mysql error info:
$mysql_error)\">email the webmaster</a>";
exit;
}
//end error function
//Then connect to your db. pconnect is persistant (Do you need a persistant
connection? If not use mysql_connect.)

$db = mysql_connect('localhost', 'yourusername', 'yourpass') ;

//Then your query
$sql = "SELECT * FROM yourdatabase";
result = mysql_query($sql,$db) or the_error("Couldn't execute get sector
types query: <pre>$sql</pre>");
^
^
Function Call

This will help you enormously for debugging . . .
-JD
Jul 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.