0

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?

3
  • You have a concatenation issue. See your answers. Commented Aug 26, 2011 at 15:22
  • why was this down voted??seems nothing wrong in this question.. Commented Aug 26, 2011 at 15:38
  • @affuzz..:thank you for the suggestion to see answers.. Commented Aug 26, 2011 at 15:52

2 Answers 2

3
$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;
Sign up to request clarification or add additional context in comments.

1 Comment

Just to clarify for Ross, you set $abc_output initially outside of the while loop and just appended all of the rows to it. Otherwise it was overwriting the variable each time.
2
$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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.