如何对齐显示的php搜索结果?

时间:2011-10-24 22:51:00

标签: php mysql html

如何在显示的数据周围对齐并放置div标签?我是非常新的php,希望你们能帮忙!

谢谢!

詹姆斯

<?php

$conn = mysql_connect("", "", "");

if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}

{

$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";

}

if (!mysql_select_db("")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}

$sql = "SELECT name,lastname,email 
FROM   test_mysql
WHERE  name LIKE '$search%' AND lastname LIKE '$searchterm'";

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}


while ($row = mysql_fetch_assoc($result)) {
echo $row["name"];
echo $row["lastname"];
echo $row["email"];
}

mysql_free_result($result);

?>

<?php echo $row["name"];?>
<br>
<?php echo $row["lastname"];?>
<br>
<?php echo $row["email"];?>

1 个答案:

答案 0 :(得分:1)

我认为你的意思是这样的:

while ($row = mysql_fetch_assoc($result)) {
   echo '<div class="data">';
   echo '<label>' . $row["name"] . '</label>';
   echo '<label>' . $row["lastname"] . '</label>';
   echo '<label>' . $row["email"] . '</label>';
   echo '</div>';
}

无论如何,我不明白需要这个:

<?php echo $row["name"];?>
<br>
<?php echo $row["lastname"];?>
<br>
<?php echo $row["email"];?>

永远不会回显任何东西(将始终回显空字符串),因为当脚本到达此代码时,$row = false