Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
idnoble
Forum Commoner
Posts: 28
Joined: Wed Mar 31, 2004 4:09 am

Help

Post by idnoble »

Hello,
I wrote a lilttle script to search a database and each time I search i always get this error message

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\phpweb\search.php on line 23

I've gone through the script over and over again but no success, can someone please help me

the script is below

Code: Select all

<?php
//Create short variable names
$search = $HTTP_POST_VARS['search'];
$search= trim($search);
if (!$search)
{
echo '<p style= "font-size: 15pt"><strong>You have not specified a search term. Please go back and try again.</strong></p>';
exit;
}
$search = addslashes($search);
$session = mysql_connect('localhost', 'root', 'platinum');
if (!$session)
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

mysql_select_db('xplosive')or die(mysql_error());
$query = "select * 
from movies, music
where Title like '%".$search."%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);

echo '<p style= "font-size: 15pt"><strong>Number of products found: '.$num_results.'</strong></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 />Genre: ';
 echo stripslashes($row['Genre']);
 echo '<br/> Synopsis: ';
 echo stripslashes ($row['Synopsis']);
 echo'<br> Price: ';
 echo stripslashes ($row['Price']);
 echo '<br/> Itemcode: ';
 echo stripslashes ($row['Price']);
 echo '</p>';
}
?>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

$result = mysql_query($query);

change

$result = mysql_query($query) or die(mysql_error());

let us know what that tells ya
idnoble
Forum Commoner
Posts: 28
Joined: Wed Mar 31, 2004 4:09 am

Post by idnoble »

thank you, i've done that and now the error message has changed to

Column: 'Title' in where clause is ambiguous

any ideas?

thank you
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Both the music and movies tables have a Tiltes column, so it doesn't know which one you are refering to in the query.
So you want something like:
$query = "select *
from movies, music
where movies.Title like '%".$search."%' OR music.Title like '%".$search."%'";
Post Reply