Searching a Database

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
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Searching a Database

Post by djwk »

Here's my code:

Code: Select all

<?php
$search = $_GET['search'];

$server = "localhost";
$user = "***";
$pass = "***";
$db = "***";

mysql_connect($server, $user, $pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

$result = mysql_query("SELECT * FROM vidz
 WHERE Artist='$search'") or die(mysql_error());  

$row = mysql_fetch_array( $result ); 
echo "<a href=watch.php?id=".$row['id'].">".$row['Artist']." - ".$row['Track']."</a><br />";

?>
If i search Rihanna ($search = "Rihanna";). There are 2 videos in the database by Rihanna. This code only returns the first one.

How would I be able to edit this code to display every video by Rihanna?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

You should read what the manual says about mysql_fetch_array()
Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Post by djwk »

Yea I fixed it just as you posted this.

Code: Select all

while($row = mysql_fetch_array($result)){

if($row['id'] == ""){

echo "No search results found.";

} else {

echo "<a href=watch.php?id=".$row['id'].">".$row['Artist']." - ".$row['Track']."</a><br />";

}

}
Post Reply