我知道rand()的顺序不是从数据库中绘制随机值的最快方法,但我的数据库很小,而且此时;我只是想让它起作用!哈哈。这是我的代码:
include('includes/dbc.php');
$top_query = "SELECT * FROM top WHERE 'occasion_id =" . $occasion . "' AND 'temperature_id = " . $temperature . "' AND 'user_id = " . $user_id . "'ORDER BY RAND() LIMIT 1";
$top_result = mysqli_query($dbc, $top_query) or die (' The top SELECT query is broken.');
mysqli_close ($dbc);
while($row= mysqli_fetch_array($top_result)) {
echo 'This top has an id of:' . $row['top_id'] . '<br> ';
echo 'Does this top require pants?' . $row['needs_pants'] . '<br>';
echo 'What\'s the colour id of this top?' . $row['colour_id'] . '<br>';
echo $row['value'];
}
由于某些原因,这只是不起作用,当我尝试运行我的数组时,我只会显示空白。它在我通过“rand()limit 1”位投入顺序之前工作,但显然我得到了每个值而不是一个随机值。
谁能看到我哪里出错了?非常感谢!
答案 0 :(得分:8)
您的查询格式错误。它不是死亡,它只是没有返回结果。
请注意错误的单引号:
$top_query = "SELECT * FROM top WHERE 'occasion_id =" . $occasion . "' AND 'temperature_id = " . $temperature . "' AND 'user_id = " . $user_id . "'ORDER BY RAND() LIMIT 1";
您可以通过输出查询然后直接在MySQL中运行它来证明这一点(例如通过PHPMyAdmin):
echo $top_query;
mysqli_error()
和mysqli_num_rows()
等功能也有助于确定查询结果。
为了让您继续前进,SQL应该更像是:
$top_query = "SELECT * FROM top WHERE occasion_id = '" . $occasion . "' AND temperature_id = '" . $temperature . "' AND user_id = '" . $user_id . "' ORDER BY RAND() LIMIT 1";
一些补充说明: