简体   繁体   中英

Random row selection using MySQL returns NULL

I am trying to get a random row from MySQL table but all three attemps:

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


give a NULL result in mysql_query($query).

Higher up my PHP code I obtain a row from the same table OK by specifying WHERE, so I don't understand why I can't retrieve a random one.

Here is the code snippet:

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

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

These queries look OK, but I think you're starting at the wrong place. When you're uncertain how to frame something in SQL, open up a SQL client like SequelPro or Navicat and try writing a few queries by hand until you get the result you want. (Also this gives you a chance to double-check the contents of relevant tables and ensure the expected data are there.) Then you can go back into the PHP with full confidence that the SQL code is correct, so if there's a problem it must be with the PHP (either the variables you inject into a Mysql statement, or the way you call that statement).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM