【发布时间】:2016-12-15 06:17:04
【问题描述】:
我正在尝试从 MySQL 获取一些数据并使用 PHP 回显它。以下是我使用的代码。请检查代码并告诉我其中有什么问题。
<?php
// Get a connection for the database
require_once('mysqli_connect.php');
// Create a query for the database
$query = "SELECT first_name, last_name, email, street, city, state, zip, phone, birth_date FROM testable";
// Get a response from the database by sending the connection and the query
$response = @mysqli_query($dbc, $query);
// If the query executed properly proceed
if($response){
echo '<table align="left"
cellspacing="5" cellpadding="8">
<tr><td align="left"><b>First Name</b></td>
<td align="left"><b>Last Name</b></td>
<td align="left"><b>Email</b></td>
<td align="left"><b>Street</b></td>
<td align="left"><b>City</b></td>
<td align="left"><b>State</b></td>
<td align="left"><b>Zip</b></td>
<td align="left"><b>Phone</b></td>
<td align="left"><b>Birth Day</b></td></tr>';
// mysqli_fetch_array will return a row of data from the query until no further data is available
while($row = mysqli_fetch_array($response)){
echo '<tr><td align="left">' .
$row['first_name'] . '</td><td align="left">' .
$row['last_name'] . '</td><td align="left">' .
$row['email'] . '</td><td align="left">' .
$row['street'] . '</td><td align="left">' .
$row['city'] . '</td><td align="left">' .
$row['state'] . '</td><td align="left">' .
$row['zip'] . '</td><td align="left">' .
$row['phone'] . '</td><td align="left">' .
$row['birth_date'] . '</td><td align="left">';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($dbc);
}
// Close connection to the database
mysqli_close($dbc);
?>
我得到的这段代码的输出是:
名字姓氏电子邮件街道城市国家邮编电话生日'; // mysqli_fetch_array 将从查询中返回一行数据 // 直到 没有更多数据可用 while($row = mysqli_fetch_array($response)){ echo '' . $row['first_name'] 。 '' 。 $row['last_name'] 。 '' 。 $row['email'] 。 '' 。 $row['street'] 。 '' 。 $row['city'] 。 '' 。 $row['state'] 。 '' 。 $row['zip'] 。 '' 。 $row['电话'] 。 '' 。 $row['birth_date'] 。 '';回声''; } 回声''; } else { echo "无法发出数据库查询";回声 mysqli_error($dbc); } // 关闭与数据库的连接 mysqli_close($dbc); ?>
【问题讨论】:
-
为什么mysqli_query前有个@
-
@ 是一个错误抑制字符
-
我正在学习 PHP,但真的不知道这个@。我按照一些教程编写了这段代码。我设法按照本教程插入数据,但无法从数据库中获取数据。我应该删除它吗?
-
我删除了@,但它仍然显示相同的输出。
-
输出表明while里面的PHP部分没有执行,只是作为文本输出。这有点奇怪,因为我没有看到任何会使此代码中断的引号。我会用你的代码做一些测试然后回来