这是我使用的脚本。
<?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秒内
答案 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();
?>
答案 1 :(得分:1)
if ($conn->query($sql) === TRUE) ..
mysqli::query
不会返回true
,这是您的整个问题。查询工作正常。对于SELECT
次查询,它会返回结果集,我认为您想要循环。请阅读此功能的手册:http://php.net/manual/en/mysqli.query.php