0

我正在尝试从 MySQL 表中获取随机行,但所有三个尝试:

$query = "SELECT cid FROM table LIMIT 1 OFFSET ".rand(1,$num_rows);
$query = "SELECT cid FROM table OFFSET RANDOM() * (SELECT COUNT(*) FROM table) LIMIT 1";
$query = "SELECT * FROM table ORDER BY RAND() LIMIT 1";


在 mysql_query($query) 中给出 NULL 结果。

在我的 PHP 代码中,我可以通过指定 WHERE 从同一个表中获取一行,所以我不明白为什么我不能检索一个随机的。

这是代码片段:

    $query = "SELECT uid,clu FROM uable WHERE un = '$un'";
    $result = mysql_query($query) or die(sqlerror(__LINE__,mysql_errno(),mysql_error()));
    $resultid = mysql_fetch_assoc($result);
    $uid = $resultid['uid'];
file_put_contents('debugging.txt',__LINE__.' - $uid = '.var_export($uid,true).PHP_EOL,FILE_APPEND);
    $query = "SELECT * FROM table WHERE uid = $uid AND cn = '$cn'";
    $result = mysql_query($query) or die(sqlerror(__LINE__,mysql_errno(),mysql_error()));
    $cr = mysql_fetch_assoc($result);
    $cid= $cr['cid'];
file_put_contents('debugging.txt',__LINE__.' - $cid= '.var_export($cid,true).PHP_EOL,FILE_APPEND);
    $query = "SELECT * FROM fable WHERE cid= '$cid'";
    $result = mysql_query($query) or die(sqlerror(__LINE__,mysql_errno(),mysql_error()));
file_put_contents('debugging.txt',__LINE__.' - $result = '.var_export($result,true).PHP_EOL,FILE_APPEND);
    $fr = mysql_fetch_assoc($result);
file_put_contents('debugging.txt',__LINE__.' - $fr = '.var_export($fr,true).PHP_EOL,FILE_APPEND);
    echo  '<form action="'.$_SERVER['PHP_SELF'].’" method="post">';
    if (!$fr) {
        $o= $cn;
        while ($o= $cn) {
//    $ac = mysql_query("SELECT * FROM table") or die(sqlerror(__LINE__,mysql_errno(),mysql_error()));
//    $num_rows = mysql_num_rows($ac);
//file_put_contents('debugging.txt',__LINE__.' - $num_rows = '.$num_rows.PHP_EOL,FILE_APPEND);
//    --$num_rows;
//    $query = "SELECT cid FROM table LIMIT 1 OFFSET ".rand(1,$num_rows);
    $query = "SELECT cid FROM table OFFSET RANDOM() * (SELECT COUNT(*) FROM table) LIMIT 1";
//        $query = "SELECT * FROM table ORDER BY RAND() LIMIT 1";
        $resultid = mysql_query($query) or die(sqlerror(__LINE__,mysql_errno(),mysql_error()));
        $opr = mysql_fetch_assoc($resultid);
        $o= $opr['cn'];
        }
file_put_contents('debugging.txt',__LINE__.' - $query = '.$query.PHP_EOL,FILE_APPEND);
file_put_contents('debugging.txt',__LINE__.' - $resultid = '.var_export($resultid,true).PHP_EOL,FILE_APPEND);
file_put_contents('debugging.txt',__LINE__.' - $op[\'cid\'] = '.$op['cid'].PHP_EOL,FILE_APPEND);
        $query = "SELECT * FROM table WHERE cid= ".$op;
        $result = mysql_query($query) or die(sqlerror(__LINE__,mysql_errno(),mysql_error()));
        $opr = mysql_fetch_assoc($opr);
        $o= $opr['cn'];
        $od= $opr['description'];
        echo  '<p>'.$op;
        if ($od<> '') {
        echo  ','.$odesc;
        }
        echo  '</p>';
        echo  '<input type="submit" name="continue" id="continue" value="Continue">';
    } else {
        echo  '<p>'.$fr['p'].'</p>';
        echo  '<input type="submit" name="continue" id="continue" value="Continue">';
    }
    echo  '</form>';

生成的debugging.txt:
24 - $uid = '4'
29 - $cid = '21'
32 - $result = NULL
34 - $fr = false

4

1 回答 1

2

这些查询看起来不错,但我认为您从错误的地方开始。当您不确定如何在 SQL 中构建某些内容时,请打开像 SequelPro 或 Navicat 这样的 SQL 客户端,并尝试手动编写一些查询,直到获得您想要的结果。(这也让您有机会仔细检查相关表的内容并确保存在预期的数据。)然后您可以完全确信 SQL 代码是正确的,因此如果出现问题,它必须使用 PHP(注入到 Mysql 语句中的变量或调用该语句的方式)。

于 2015-04-29T17:58:46.233 回答