-4

Possible Duplicate:
PHP (MySQL) error : “Warning: mysql_num_rows() expects parameter 1 to be resource”

I'm trying to get this php page of an AJAX function to work.

Here is the error message when I try to run it:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/ftpbpan/public_html/ThornAJAX/info.php on line 10

Line 10 being:

**$num_rows = mysql_num_rows($query);**

<?php
$connect = mysql_connect("localhost","****","******") or die("didnot connect to database");

mysql_select_db("people"); 

if(strlen($_GET['user']) >0){
$name = $_GET['user'];
$query = mysql_query("select * from info where name='$name' limit 1");
$num_rows = mysql_num_rows($query);

if($num_rows == 1) {
  $row = mysql_fetch_assoc($query);
Community
  • 1
  • 1
  • 2
    Have you checked any of the links on the right-hand side? – jeroen Nov 08 '12 at 21:32
  • mysql_query returns a result on success and false on error. I would say you have an error in your query. `mysql_` library is also deprecated. – Jrod Nov 08 '12 at 21:33
  • Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/KJveJ). See the *[red box](http://goo.gl/GPmFd)*? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/3gqF9) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/vFWnC). – John V. Nov 08 '12 at 21:37
  • -1. The error message should be preceded by another error such as the dreaded _you have an error in your query_. Post _that_ error. – Salman A Nov 08 '12 at 21:45
  • put an `or die(mysql_error())` on your query line to find out exactly WHY it's returning a boolean false to $query. Then go read up about [sql injection attacks](http://bobby-tables.com) and enjoy having your server destroyed if you don't patch the holes. – Marc B Nov 08 '12 at 21:54

1 Answers1

0

If you are using LIMIT 1, can't you assume that there will either be 1 row or 0 rows? Therefore, if I were you and I was using a deprecated method to interact with MySQL databases, I would use:

$query = mysql_query("SELECT * FROM info WHERE name='$name' LIMIT 1")or die(mysql_error());
if ($query && mysql_num_rows($query)) {
    // A result was found
    $row = mysql_fetch_array($query);
} else {
    //There were no results found
}

Then again, you should probably read this:

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
burmat
  • 2,468
  • 1
  • 21
  • 28
  • Thanks for all of your help guys...Sorry, I'm a complete newbie at this. Here is the page I'm working on: http://www.bpanzullo.com/ThornAJAX/Ajaxindex.html I only put the name Paul into the database for testing This one helped me narrow down the error, I tried mysqli instead of msql, but created more errors, I'm getting a no users name Paul in the database error, Paul is the ONLY person in the database I put in to test this. The database is called people, the table I'm using (there are many) is called peopleTwo, here is the code that's ALMOST working: – Bob Panzullo Nov 10 '12 at 15:30
  • 0){ $name = $_GET['user']; $query = mysql_query("select * from peopleTwo where name='$name' limit 1"); if (false === $query) { die(mysql_error()); } $num_rows = mysql_num_rows($query); if($num_rows == 1) { $row = mysql_fetch_assoc($query); – Bob Panzullo Nov 10 '12 at 15:31