无法将SQL数据库表显示到PHP页面

时间:2015-03-08 05:28:03

标签: php mysql sql-server database datatables

我正在尝试在我的数据库中显示其中一个表的某一行。

Here is my code for it:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
else printf("Connection Successful");

mysql_select_db('magento');
$query = "SELECT * FROM poll_answer"; 
$result = mysqli_query($query);


while($poll=mysqli_fetch_assoc($result)){
    echo "<tr>" .$poll['answer_title']. "</td>";
}

这是我的表:

+--------------+------------------+------+-----+---------+----------------+
| Field        | Type             | Null | Key | Default | Extra          |
+--------------+------------------+------+-----+---------+----------------+
| answer_id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| poll_id      | int(10) unsigned | NO   | MUL | 0       |                |
| answer_title | varchar(255)     | YES  |     | NULL    |                |
| votes_count  | int(10) unsigned | NO   |     | 0       |                |
| answer_order | smallint(6)      | NO   |     | 0       |                |
+--------------+------------------+------+-----+---------+----------------+

我无法打印任何东西,但是&#34;连接成功&#34;!

谁能告诉我可能有什么问题?

EDITED SQL到SQLI,仍然是同样的问题

3 个答案:

答案 0 :(得分:2)

您正在混合mysqlmysqli

更改

$result = mysql_query($query);
while($poll=mysql_fetch_assoc($result)){

$result = mysqli_query($conn , $query);
while($poll=mysqli_fetch_assoc($result)){

Check Manual

答案 1 :(得分:2)

您必须$connmysqli_query.

的连接类对象
$result = mysqli_query($conn,$query);

答案 2 :(得分:0)

可能是因为您正在使用mysql和mysqli函数。此外,很快就会弃用mysql,你可能想要切换到PDO语句,它很容易设置,这里有一个你可以很快应用的教程Link