Php mysql question

I am new to this, and I am stumed on what seems to be a stupid problem, that I just can’t figure out. I am searching a mysql database. Everything so far is fien but I am geting an error. The error reads: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/digitalo/public_html/exp/bookorama/results.php on line 32

Not sure why this is happening, everything looks iek it shoudl work. I am getting this code from a book.

<html>
<head>
<title>Search Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Book-O-Rama Search Results<br>
<?

  trim($searchterm);
  if (!$searchtype || !$searchterm){
  	echo "You did not fill our the required fields<br>";
	exit;
	}
	
	$searchtype = addslashes($searchtype);
	$searchterm = addslashes($searchterm);
	
  @ $db = mysql_pconnect("localhost", "USERNAME", "USERPASS");

  if (!db){
	echo "Error: COuld notconnect to database.";
	exit;
	}

  mysql_select_db("digitalo_bookstore");
  $query = "select * from books where ".$searchtype." like 
  		'%".$searchterm."%'";

  $result = mysql_query($query);
  $num_results = mysql_num_rows($result);

  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 htmlspecialchars (stripslashes($row["author"]));
	echo "<br>ISBN: ";
	echo htmlspecialchars (stripslashes($row["isbn"]));
	echo "<br>Price: ";
	echo htmlspecialchars (stripslashes($row["price"]));
	echo "</p>";
  }
?>

</body>
</html>

the error is on line :

 $num_results = mysql_num_rows($result);

Don’t understand why I am getting this error.

Thanks

that type of error usually happens when the query is unsuccessful. Try using this code, and see if your query actually gets executed.

<html>
<head>
<title>Search Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Book-O-Rama Search Results<br>
<?

  trim($searchterm);
  if (!$searchtype || !$searchterm){
  	echo "You did not fill our the required fields<br>";
	exit;
	}
	
	$searchtype = addslashes($searchtype);
	$searchterm = addslashes($searchterm);
	
  @ $db = mysql_pconnect("localhost", "USERNAME", "USERPASS");

  if (!$db){
	echo "Error: COuld notconnect to database.";
	exit;
	}

  mysql_select_db("digitalo_bookstore");
  $query = "select * from books where ".$searchtype." like 
  		'%".$searchterm."%'";

  $result = mysql_query($query);

  // ------ Added this to check if query is successful----------------
  if($result)
  {
     print "query successful.";
  }
  else
  {
      print "Something goofed up.";
  }
  // --------------------------------------------------------------------------------  

  $num_results = mysql_num_rows($result);

  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 htmlspecialchars (stripslashes($row["author"]));
	echo "<br>ISBN: ";
	echo htmlspecialchars (stripslashes($row["isbn"]));
	echo "<br>Price: ";
	echo htmlspecialchars (stripslashes($row["price"]));
	echo "</p>";
  }
?>

</body>
</html>

Also, there was a slight error in your if(!db) statement… it should be:

if(!$db)

you need that $ there, because thats how php recognizes its variables.

The problem could be that your not even connecting to you database.

Also, be sure to close the connection

Let me know if that helps…

Cheers,
Jubs :cowboy:

One more thing…

If you are really trying to learn and you’re serious about it, then get these two books:

PHP: http://www.amazon.com/exec/obidos/tg/detail/-/0764535617/qid=1053751242/sr=8-1/ref=sr_8_1/002-2551283-1162431?v=glance&s=books&n=507846

mySQL: http://www.amazon.com/exec/obidos/tg/detail/-/0764516922/qid=1053751276/sr=1-2/ref=sr_1_2/002-2551283-1162431?v=glance&s=books

They are priceless. They have helped me so much. All of my knowledge of PHP and mySQL manipulation pretty much began with that and in just a week or so I was create full control panels for websites. Together the price is about $50 but don’t whine about spending the money because they are worth every penny. mdipicom bought the PHP after I recommended it to him and he loved it. If you have 3/4 of a brain cell you will understand PHP when you get done with those books.

Actually the first one is all you really need because it actually explains the basics of php/mysql all in one, but the second book helps with more specific areas of the mysql.

:slight_smile:

thanks a lot for the info, its late so i wont be able to check the code until tomorrow, but i’ll def let you know if it works. yea im not sure if i am even getting a connection to my db, but i overlooked the error you pointed out, so that is probably what is wrong. I know asp pretty well, but php just seems so much more powerful that i had to try and learn it, and its really not much different than connecting to db’s using asp, same concept. i will take a look at those books, i just bought one called php and mysql web development by SAMS. i dont mind spending $$ on this stuff, as this is my how i make my $$ :slight_smile: gotta lose money to make money…

anyway thanks again, and ill let you know the verdict tom…

much appreciated—digital

ok well i just couldnt wait to test it, it seems that i am not connecting to my database, not sure why. i am going to take a step back and see what the problem is.

try changing this:


@ $db = mysql_pconnect("localhost", "digitalo_digital","ny11204");

to


$db = @mysql_pconnect("localhost", "digitalo_digital","ny11204");

that might work…

oh, and change this:


mysql_select_db("digitalo_bookstore");

to


mysql_select_db("digitalo_bookstore", $db);

ok i am not connecting to my db, and i am also gettng that same error. starting to piss me off. could this have to do with the creation of the database? this is my first time building a mysql db as well as accessing it from the web. also this is running off a web server, not my pc. any configuring i should know about?

thanks

not really, but you might want to check with your server to see how they want you to connect. Sometimes local host might not work…

also, are you sure the user and pass are correct? because you usualy would have to create a user specifically for that database. Your username and password for your actual website wouldn’t work in this case…

i would definately check with your server. :slight_smile:

i checked username and password, they are correct. web server says to use localhost.

i went back and tryed to trouble shoot my original script. with your corrections here is what i got up to.


<html>
<head>
<title>Search Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Book-O-Rama Search Results
<?

  trim($searchterm);
  if (!$searchtype || !$searchterm){
      echo "You did not fill our the required fields";
    exit;
    }
    
    $searchtype = addslashes($searchtype);
    $searchterm = addslashes($searchterm);
    
  @ $db = mysql_pconnect("localhost", "digitalo_digital","ny11204");

  if(!$db){
    echo "Error: Could notconnect to database.";
    exit;
    }

  mysql_select_db("digitalo_bookstore");
  $query = "select * from books where ".$searchtype." like 
          '%".$searchterm."%'";

 
?>

</body>
</html>

it seems i am now successfully coneecting to the db i created. but i keep getting the original error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/digitalo/public_html/exp/bookorama/results.php on line 32

looked this error up on internet, came up with nothing. but it seems liek many people get this error.

any other ideas?

btw. here is a link to the actualy page.
http://www.digitalosophy.com/exp/bookorama/search.html[URL=http://www.digitalosophy.com/exp/bookorama/search.html]

thanks

do you have any values in the database? does the error come up if you enter data that does exist? doesn’t exist?

do you have AIM? if you do can you PM me your screen name. This might be easier if I could talk in real-time :slight_smile:

oh, did you try to do that check to see if the query was successful?

i pmed you my aim sn. whats interesting is when i run query’s from the db its self everything works fine. but when i try to access via php there is a problem

<html>
<head>
<title>Search Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Book-O-Rama Search Results
<?

  trim($searchterm);
  if (!$searchtype || !$searchterm){
      echo "You did not fill our the required fields";
    exit;
    }
    
    $searchtype = addslashes($searchtype);
    $searchterm = addslashes($searchterm);
    
  @ $db = mysql_pconnect("localhost", "digitalo_digital", "ny11204");

  if (!$db){
    echo "Error: COuld notconnect to database.";
    exit;
    }

  mysql_select_db("digitalo_bookstore");
  $query = "select * from books where ".$searchtype." like '%$searchterm%' ";

  $result = mysql_query($query);

  // ------ Added this to check if query is successful----------------
  if($result)
  {
     print "query successful.";
  }
  else
  {
      print "Something goofed up.";
  }
  // --------------------------------------------------------------------------------  

  $num_results = mysql_num_rows($result);

  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>Author: ";
    echo htmlspecialchars (stripslashes($row["author"]));
    echo "ISBN: ";
    echo htmlspecialchars (stripslashes($row["isbn"]));
    echo "Price: ";
    echo htmlspecialchars (stripslashes($row["price"]));
    echo "</p>";
  }
?>

</body>
</html>


<html>
<head>
<title>Search Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Book-O-Rama Search Results
<?

  trim($searchterm);
  if (!$searchtype || !$searchterm){
      echo "You did not fill our the required fields";
    exit;
    }
    
    $searchtype = addslashes($searchtype);
    $searchterm = addslashes($searchterm);
    
  @ $db = mysql_pconnect("localhost", "digitalo_digital", "ny11204");

  if (!$db){
    echo "Error: COuld notconnect to database.";
    exit;
    }

  mysql_select_db("digitalo_bookstore");
  $query = "select * from books where ".$searchtype." like '%$searchterm%' ";

  $result = mysql_query($query);

  // ------ Added this to check if query is successful----------------
  if($result)
  {
     print "query successful.";
  }
  else
  {
      print "Something goofed up.<br />";
      print mysql_error($db);
      exit;
  }
  // --------------------------------------------------------------------------------  

  $num_results = mysql_num_rows($result);

  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>Author: ";
    echo htmlspecialchars (stripslashes($row["author"]));
    echo "ISBN: ";
    echo htmlspecialchars (stripslashes($row["isbn"]));
    echo "Price: ";
    echo htmlspecialchars (stripslashes($row["price"]));
    echo "</p>";
  }
?>

</body>
</html>

For anyone following this thread i didn’t add the user to the database …doh!!! :frowning:

the last code is correct

Yeah, and the error that he was recieving was:

No Database Selected

So if you know your code is right and you are getting that error, then check to make sure that you added the user to the database. :slight_smile: