Ross Taylor
Ross Taylor

Reputation: 109

PHP help -- displaying multiple rows

I am fetching some data from a database like this

$sql ="SELECT * FROM table WHERE field1 LIKE '%$name%'";

And the result of this is many rows, how can i display all the rows using PHP?

If its just for one row i am using this:

$sql ="SELECT * FROM table WHERE field1 LIKE '%$name%'";       
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$abc_output= "Data Fetched <br />";      
$abc_output .="name: " . $row['name'] . "<br />" ;
$abc_output .="age: " . $row['age'] . "<br />" ; 
$abc_output .="sex: " .  $row['sex'] . "<br />" ; 
$abc_output .='<a href="abc.com"> Back to Main Menu</a>';
 }
echo $abc_output;

How can i display for multiple rows?

Upvotes: 0

Views: 41

Answers (2)

Doug
Doug

Reputation: 6442

$sql ="SELECT * FROM table WHERE field1 LIKE '%$name%'";       
$result = mysql_query($sql);

$abc_output= "Data Fetched <br />"; 

while($row = mysql_fetch_array($result))
{
    $abc_output .="name: " . $row['name'] . "<br />" ;
    $abc_output .="age: " . $row['age'] . "<br />" ; 
    $abc_output .="sex: " .  $row['sex'] . "<br />" ; 
    $abc_output .="<hr />";
}

$abc_output .='<a href="abc.com"> Back to Main Menu</a>';
echo $abc_output;

Upvotes: 3

Marc B
Marc B

Reputation: 360572

$abc_output= "Data Fetched <br />";      

This'll reset your string on every row you fetch. You need to concatenate the string everywhere within the loop, or you'll just delete all the previous work:

$abc_output .= "Data Fetched <br />";      
            ^--add this.

Upvotes: 2

Related Questions