[英]Mysqli query not returning any information?
Im attempting to find the next date. 我正在尝试寻找下一个日期。 For what ever reason, the below code is not returning any information.
无论出于何种原因,以下代码都不会返回任何信息。 I have 1 entry formatted as such : 2017-03-18 12:37:00.... what am I doing wrong?
我有1个条目的格式如下:2017-03-18 12:37:00 ....我在做什么错?
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'user');
define('DB_PASSWORD', 'pass');
define('DB_DATABASE', 'db');
$link = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
$query = "SELECT * FROM group WHERE date_time >= NOW() order by date_time LIMIT 1";
$next = mysqli_query($link, $query);
echo $next;
?>
You need to call a fetch function to get the data from the query. 您需要调用获取函数以从查询中获取数据。 You should also check if it found anything;
您还应该检查它是否发现了任何东西。 the
fetch
function will return null
instead of an array if no rows were matched by the query. 如果查询未匹配任何行,则
fetch
函数将返回null
而不是数组。
$query = "SELECT * FROM `group` WHERE date_time >= NOW() ORDER BY date_time LIMIT 1";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_assoc($result);
if ($row) {
$date_time = $row['date_time'];
echo "Next date is " . $date_time;
} else {
echo "No time after now in the table.";
}
With your code you return your query pointer. 使用您的代码,您可以返回查询指针。 You're missing one line to fetch the entries.
您缺少一行来提取条目。
https://secure.php.net/manual/de/mysqli-result.fetch-assoc.php https://secure.php.net/manual/de/mysqli-result.fetch-assoc.php
Here is a good example how to achieve that. 这是一个很好的例子。
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
$result->free();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.