Page 1 of 1

Searching a Database

Posted: Fri Jun 08, 2007 3:48 am
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?

Posted: Fri Jun 08, 2007 4:17 am
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.

Posted: Fri Jun 08, 2007 4:20 am
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 />";

}

}