Using PHP to query Mysql and displaying the results

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

Using PHP to query Mysql and displaying the results

Post by idnoble »

hello,
I created this php script to search for products and display the results in the browser, but each time I run it, it always displays everything in my database and not just what i requested, i've checked it and can't find anything wrong, please help i'm buggered.
here's the script:

Code: Select all

<?php 

$Search = $HTTP_POST_VARS['Search']; 

$session = mysql_connect ("localhost", "root", "platinum"); 

if (!$session) 
{ 
echo 'Error: Could not connect to the database. Please try again later.'; 
exit; 
} 
$Search= trim($Search); 

$Search = addslashes($Search); 

$session = mysql_connect("localhost", "root", "platinum"); 
if (!$session) 
{ 
echo 'Error: Could not connect to the database. Please try again later.'; 
exit; 
} 

mysql_select_db('xplosive'); 
$query = "select * from movies where Title like '".$Search."%'"; 
$result = mysql_query($query); 

$num_results = mysql_num_rows($result); 

echo '<p> Number of products 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 />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
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

SQL Manual: look for LIMIT or WHERE - depending on what you want.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Echo the query just before you run it. The way it's set up, the query will return everything if $Search is empty.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply