我从像这样的数据库中获取一些数据
$sql ="SELECT * FROM table WHERE field1 LIKE '%$name%'";
结果是很多行,如何使用PHP显示所有行?
如果它只用于一行我使用它:
$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;
如何显示多行?
答案 0 :(得分: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;
答案 1 :(得分:2)
$abc_output= "Data Fetched <br />";
这将在你获取的每一行上重置你的字符串。你需要在循环中的任何地方连接字符串,或者你只是删除以前的所有工作:
$abc_output .= "Data Fetched <br />";
^--add this.