# Php mysql question

**URL:** <https://forum.kirupa.com/t/php-mysql-question/22193>\
**Category:** Uncategorized\
**Created:** [May 31, 2003, 8:33pm UTC](https://forum.kirupa.com/t/php-mysql-question/22193 "2003-05-31T20:33:01Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 31, 2003, 8:33pm UTC](https://forum.kirupa.com/t/php-mysql-question/22193/1 "2003-05-31T20:33:01Z")

</div>

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.

```php
<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 :

```php
 $num_results = mysql_num_rows($result);

```

Don’t understand why I am getting this error.

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 1, 2003, 4:59am UTC](https://forum.kirupa.com/t/php-mysql-question/22193/2 "2003-06-01T04:59:11Z")

</div>

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

```php
<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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 1, 2003, 5:00am UTC](https://forum.kirupa.com/t/php-mysql-question/22193/3 "2003-06-01T05:00:41Z")

</div>

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](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](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.

🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 1, 2003, 9:17am UTC](https://forum.kirupa.com/t/php-mysql-question/22193/4 "2003-06-01T09:17:33Z")

</div>

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 $$ 🙂 gotta lose money to make money…

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

much appreciated—digital

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 1, 2003, 9:23am UTC](https://forum.kirupa.com/t/php-mysql-question/22193/5 "2003-06-01T09:23:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 1, 2003, 1:16pm UTC](https://forum.kirupa.com/t/php-mysql-question/22193/6 "2003-06-01T13:16:36Z")

</div>

try changing this:

```php

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

```

to

```php

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

```

that might work…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 1, 2003, 1:24pm UTC](https://forum.kirupa.com/t/php-mysql-question/22193/7 "2003-06-01T13:24:22Z")

</div>

oh, and change this:

```php

mysql_select_db("digitalo_bookstore");

```

to

```php

mysql_select_db("digitalo_bookstore", $db);

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 2, 2003, 12:05am UTC](https://forum.kirupa.com/t/php-mysql-question/22193/8 "2003-06-02T00:05:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 2, 2003, 12:13am UTC](https://forum.kirupa.com/t/php-mysql-question/22193/9 "2003-06-02T00:13:50Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 2, 2003, 12:28am UTC](https://forum.kirupa.com/t/php-mysql-question/22193/10 "2003-06-02T00:28:04Z")

</div>

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.

```php

<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]](http://www.digitalosophy.com/exp/bookorama/search.html)

thanks

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 2, 2003, 12:34am UTC](https://forum.kirupa.com/t/php-mysql-question/22193/11 "2003-06-02T00:34:33Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 2, 2003, 12:38am UTC](https://forum.kirupa.com/t/php-mysql-question/22193/12 "2003-06-02T00:38:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 2, 2003, 1:03am UTC](https://forum.kirupa.com/t/php-mysql-question/22193/13 "2003-06-02T01:03:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 2, 2003, 1:22am UTC](https://forum.kirupa.com/t/php-mysql-question/22193/14 "2003-06-02T01:22:16Z")

</div>

```php
<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>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 2, 2003, 1:26am UTC](https://forum.kirupa.com/t/php-mysql-question/22193/15 "2003-06-02T01:26:08Z")

</div>

```php

<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>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 2, 2003, 6:46pm UTC](https://forum.kirupa.com/t/php-mysql-question/22193/16 "2003-06-02T18:46:45Z")

</div>

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

the last code is correct

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 4, 2003, 6:57pm UTC](https://forum.kirupa.com/t/php-mysql-question/22193/17 "2003-06-04T18:57:19Z")

</div>

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. 🙂
