0

i have multiple entries with starting with the letter 'a' .. but the query below only displays one result.

<?php


mysql_connect("localhost", "Master", "pword");
mysql_select_db("db");

$letter = htmlentities($_GET['search']); 
$user = mysql_query("SELECT DISTINCT keywords FROM Stacks WHERE keywords LIKE '$letter%' ORDER BY keywords")or die (mysql_error());  
 

while($rowz = mysql_fetch_array($user)){
$keyword = $rowz['keywords'];
 $name = $rowz['name'];                                         
$bad = mysql_query("SELECT * FROM Stacks WHERE keywords LIKE '$letter%' ORDER BY keywords")or die (mysql_error()); 
$num_rows = mysql_num_rows($bad)or die (mysql_error());  
}

if ($num_rows == 0)
{
echo "<font face='Courier New' font size=18px font color=#FF9900>$letter</font><br /><br />";
echo "<font face='Courier New' font size=3px font color=#FBB917>No Stacks</font><br>";
}
else 
{

	


	

echo "<font face='Courier New' font size=18px font color=#FF9900>$letter</font><br /><br />";

	

	

echo "<font face='Courier New' font size=3px font color=#FBB917><a href='stack.php?search=$keyword&submit=Go!' style='text-decoration: none';>$keyword</a></font><br>";

	

}

?>

Edited by MDanz: n/a

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by MDanz
0

Not sure but the

print_r()

Command is useful to print out the contents of an array, the best usage is

echo "<pre>";
print_r($ArrayName);
echo "</pre>";
0

Not sure but the

print_r()

Command is useful to print out the contents of an array, the best usage is

echo "<pre>";
print_r($ArrayName);
echo "</pre>";

i tried that, didn't work. so i changed the query to SELECT DISTINCT keywords and it still displays one result...

0

Ahhh I see now, every time it loops its resetting the variable.
Try this code instead:

<?php


mysql_connect("localhost", "Master", "pword");
mysql_select_db("db");

$letter = htmlentities($_GET['search']); 
$user = mysql_query("SELECT DISTINCT keywords FROM Stacks WHERE keywords LIKE '$letter%' ORDER BY keywords")or die (mysql_error());  
 
$keyword = '';
$name = '';
while($rowz = mysql_fetch_array($user)){
$keyword .= $rowz['keywords'];
 $name .= $rowz['name'];                                         
$bad = mysql_query("SELECT * FROM Stacks WHERE keywords LIKE '$letter%' ORDER BY keywords")or die (mysql_error()); 
$num_rows = mysql_num_rows($bad)or die (mysql_error());  
}

if ($num_rows == 0)
{
echo "<font face='Courier New' font size=18px font color=#FF9900>$letter</font><br /><br />";
echo "<font face='Courier New' font size=3px font color=#FBB917>No Stacks</font><br>";
}
else 
{

	


	

echo "<font face='Courier New' font size=18px font color=#FF9900>$letter</font><br /><br />";

	

	

echo "<font face='Courier New' font size=3px font color=#FBB917><a href='stack.php?search=$keyword&submit=Go!' style='text-decoration: none';>$keyword</a></font><br>";

	

}

?>

The key bit here is that I defined the variables outside the loop

$keyword = '';
$name = '';

then used the dot-equals syntax to add to the variables

$keyword .= $rowz['keywords']." ";
 $name .= $rowz['name']." ";

See if that helps

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.