mysqli_fetch_array不起作用

时间:2017-01-22 01:33:17

标签: php

$connection = mysqli_connect($server, $user_name, $password);

if ($connection) {
   echo "Database Found ";
}
else {
 die("Database connection failed");
}
$query = "SELECT Topic, PostsMsg, DatePosted FROM posta";

$myData = mysqli_query($connection, $query);


echo "<table border=1 bordercolor=#ffffff>
<tr><th>Topic</th><th>Comments</th><th>Date</th>
</tr>";
while ($posts = mysqli_fetch_array($myData, MYSQLI_BOTH) ){
    echo "<tr>";
    echo "<td>" . $posts['Topic'] . "</td>";
    echo "<td>" . $posts['PostsMsg'] . "</td>";
    echo "<td>" . $posts['DatePosted'] . "</td>";
    echo "</tr>";
 }

echo "</table>";
mysqli_close($connection);

以上是我的代码,它正在创建此错误消息:

  

警告:mysqli_fetch_array()要求参数1为mysqli_result,在C:\ wamp \

中给出布尔值

1 个答案:

答案 0 :(得分:0)

使用预准备语句来阻止sql injection。在上面的代码中你忘记指定数据库名称。所以试试这样......

<?php
$connection = mysqli_connect($server, $user_name, $password,$database);

if ($connection) {
   echo "Database Found ";
}
else {
 die("Database connection failed");
}
$query = "SELECT Topic, PostsMsg, DatePosted FROM posta";

$myData = mysqli_query($connection, $query);


echo "<table border=1 bordercolor=#ffffff>
<tr><th>Topic</th><th>Comments</th><th>Date</th>
</tr>";
while ($posts = mysqli_fetch_assoc($myData)){
    echo "<tr>";
    echo "<td>" . $posts['Topic'] . "</td>";
    echo "<td>" . $posts['PostsMsg'] . "</td>";
    echo "<td>" . $posts['DatePosted'] . "</td>";
    echo "</tr>";
 }

echo "</table>";
mysqli_close($connection);