mysqli SELECT查询无法正常工作,原因不明

时间:2015-07-19 18:28:09

标签: php mysql mysqli

这是我用于连接数据库并查询表" userActivityTime"的php,其中有一行。我没有连接到数据库的麻烦(即没有错误),但我的查询不起作用,尽管在互联网上看,我无法弄清楚为什么。希望你们都能提供帮助。非常感谢提前!

<?php

// ESTABLISH TABLE AND COLUMN NAMES

$mysqli = new mysqli("*****", "****", "*****", "****");

// MAKE SURE CONNECTION SUCCEEDED

if ($mysqli_connection->connect_error) {
    echo "Not connected, error: " . $mysqli_connection->connect_error;
    exit();
} else {
    echo "connected";
}

$query = "SELECT 'userDailyTime' FROM 'userActivityTime'";
if ($mysqli->query($query)) {
    echo $mysqli->error;
}


exit();

?>

3 个答案:

答案 0 :(得分:2)

如果需要,请使用反向标记而不是单引号:

SELECT `userDailyTime` FROM `userActivityTime`

或只是

SELECT userDailyTime FROM userActivityTime

修改

来自互联网的一个例子,将你的东西塞进概念中。

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

答案 1 :(得分:1)

您不需要将表名和列名放在引号中。您告诉它在查询成功时回显$mysqli->error(前面带有感叹号)。另外,您创建了$mysqli,然后将其称为$mysqli_connection,因此我不确定您的代码是如何工作的。

答案 2 :(得分:1)

试试这个:

<?php

// ESTABLISH TABLE AND COLUMN NAMES

$mysqli = new mysqli("*****", "****", "*****", "****");

// MAKE SURE CONNECTION SUCCEEDED

if ($mysqli->connect_errno) {
    echo "Not connected, error: " . $mysqli->connect_error;
    return false;
} else {
    echo "connected";
}

$query = "SELECT userDailyTime FROM userActivityTime";
if (!$mysqli->query($query)) {
    echo $mysqli->error;
}
return true;

?>