开发者

PHP help -- displaying multiple rows

开发者 https://www.devze.com 2023-03-31 14:30 出处:网络
I am fetching some data from a database like this $sql =\"SELECT * FROM table WHERE field1 LIKE \'%$name%\'\";

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?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消