我遇到一个问题,显示我编写的脚本。我无法获得表中显示的代码。我究竟做错了什么?无法在某处找到答案......
错误代码:警告:mysqli_fetch_array()期望参数1为mysqli_result,布尔值在
中给出$con=mysqli_connect("localhost","database_connected","password");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM links");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['links'] . "</td>";
echo "<td>" . $row['url'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
答案 0 :(得分:0)
我建议远离mysqli并改用PDO;除了拥有更明智的模型外,安全使用起来也相当容易一些。以下是一个快速示例,说明如何将上面的代码重写为PDO代码:
<?php
$db = new PDO('mysql:host=localhost;dbname=SOME_DB_NAME_HERE;charset=utf8', 'database_connected', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
try {
$rows = $db->query('SELECT * FROM links');
} catch(PDOException $ex) {
echo "An Error occured: ", $ex->getMessage(); //user friendly message
exit();
}
foreach($rows as $row) {
echo '<tr>';
foreach($row as $column=>$value) { echo '<td>', $value, '</td>'; }
echo '</tr>';
}
有一个相当不错的教程可以帮助您学习将mysql(i)构造映射到the HashPHP wiki的等效PDO。