English 中文(简体)
使用 ORDER 由 RAND () 使用, 多个条款处
原标题:Using ORDER BY RAND() with multiple WHERE clauses mysql

我知道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 ];
}

出于某种原因,这不起作用, 当我尝试运行我的阵列时, 我就会白白露面。 在我按“ 兰德( ) 限制 1 ” 键按顺序排列之前, 它就起作用了, 但显然我得到了每一个价值, 而不是一个随机值 。

有人看到我哪里出错了吗?

最佳回答

您的查询格式错误。 它不是 dying , 它只是没有返回结果 。

请注意错误的单引号:

$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";

一些补充说明:

  • Read up on SQL Injection.
  • Familiarize yourself with when you need to quote literal values in MySQL. In your case these are all ID columns (theoretically integers) and therefore don t need to be quoted.
  • Be mindful not to prematurely close your MySQL connection. PHP will actually do this automatically at the end of the script.
  • As commented by webbiedave, also look into Prepared Statements.
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...