SQL语法错误select * from

时间:2015-02-17 18:35:34

标签: php mysql mysqli

我的代码抛出了这个错误:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-contact-info' at line 1

我的代码:

<?php

    //connect
    $connection = mysqli_connect("myh","myu","myp","mydb") or die("Error " . mysqli_error($connection));

    //consultation: 
    $query = "SELECT * FROM web-contact-info"; 

    //execute the query. 
    $result = mysqli_query($connection, $query);
    if (!$result) {
        printf("Error: %s\n", mysqli_error($connection));
        exit();
    }

    //display information: 
    while($row = mysqli_fetch_array($result)) { 
      echo $row["live_name"] . "<br>"; 
    } 

    ?>

我试图在web-contact-info附近加上引号并得到一个稍微不同的错误:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''web-contact-info'' at line 1

我写错了什么?

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

SELECT * FROM `web-contact-info`

由于不推荐使用mysql_ *,请考虑切换为mysqliPDO

答案 1 :(得分:0)

在这样的简单引号中尝试使用表的名称

$query = "SELECT * FROM `web-contact-info`";