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>";

	

}

?>

Recommended Answers

All 4 Replies

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>";

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...

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

thanks!! works perfectly!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.