无法通过PHP查询MySQL数据库

时间:2015-12-11 16:23:45

标签: php mysql

在我的PHP代码中,我很容易将记录写入我的数据库,但由于某种原因,我无法读取任何信息。我的PHP代码是:

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM companies";

if ($conn->query($sql) === TRUE)
{
    echo "query success";

    while($row = $result->fetch_assoc())
    {
        echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
    }
}
else
{
    echo "query failure";
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$sql = "INSERT INTO companies (name) 
        VALUES ('mycompany')";

if ($conn->query($sql) === TRUE)
{

    echo "insert success";
}
else
{
    echo "insert failure";
    echo "Error: " . $sql . "<br>" . $conn->error;
}

我运行时从浏览器获得的输出是: query failureError: SELECT * FROM companies insert success

我在$ sql字符串中尝试过撇号,插入符号和引号的变体。我已经尝试在HeidiSQL中运行此查询,但它运行正常。我出错的任何想法?有什么更基本的建议,我可以尝试缩小问题的根源吗?

谢谢!

4 个答案:

答案 0 :(得分:6)

mysqli->query()SELECT语句一起使用会返回mysqli_result的实例。它与true(=== true)不同,但也不代表错误。

此外,$result未定义。

请改用:

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM companies";

if (($result = $conn->query($sql)) !== FALSE)
{
    echo "query success";

    while($row = $result->fetch_assoc())
    {
        echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
    }
}
else
{
    echo "query failure";
    echo "Error: " . $sql . "<br>" . $conn->error;
}
...

这只会将您的=== TRUE支票更改为!== FALSEMySQLi::query()失败时返回布尔值FALSE,没有结果集的成功查询时返回布尔值TRUE,结果集成功时返回mysqli_result。 这也将query()的结果分配到$result

答案 1 :(得分:1)

您尚未将查询结果分配给$result变量。

    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "SELECT * FROM companies";
    $result = $conn->query($sql);
    if ($result === TRUE)
    {
        echo "query success";

        while($row = $result->fetch_assoc())
        {
            echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
        }
    }
    else
    {
        echo "query failure";
        echo "Error: " . $sql . "<br>" . $conn->error;
    }


    $sql = "INSERT INTO companies (name) 
            VALUES ('mycompany')";

    if ($conn->query($sql) === TRUE)
    {

        echo "insert success";
    }
    else
    {
        echo "insert failure";
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

答案 2 :(得分:1)

问题是 SELECT 查询将在SUCCESS上返回 mysqli_result 对象而不是布尔值TRUE。只有在查询失败时,它才会返回一个布尔值FALSE;

因此你应该像这样使用它:

$result = $conn->query($sql);

if ($result !== FALSE){
    while($row = $result->fetch_assoc())
    {
        echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
    }
}

答案 3 :(得分:1)

只需更改if条件: -

if ($result = $conn->query($sql))
{
    echo "query success";

    while($row = $result->fetch_assoc())
    {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
}