对PHP的明显限制

时间:2015-02-10 08:25:27

标签: php mysql sql

这是我使用的脚本。

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "merlin";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}    
$time_start = microtime(true);
$sql = "SELECT Distinct host_name FROM report_data limit 5";
$time_end = microtime(true);
if ($conn->query($sql) === TRUE) {
echo "Job Done ";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}   
$time = $time_end - $time_start;
echo "In $time seconds\n";       
$conn->close();
?>     

输出:

错误:SELECT Distinct host_name FROM report_data limit 5 在0秒内

2 个答案:

答案 0 :(得分:1)

    <?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 Distinct host_name FROM report_data limit 5";
$result = $conn->query($sql);

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

http://www.w3schools.com/php/php_mysql_select.asp

答案 1 :(得分:1)

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

mysqli::query不会返回true,这是您的整个问题。查询工作正常。对于SELECT次查询,它会返回结果集,我认为您想要循环。请阅读此功能的手册:http://php.net/manual/en/mysqli.query.php