php - Random row selection using MySQL returns NULL -


i trying random row mysql table 3 attemps:

$query = "select cid table limit 1 offset ".rand(1,$num_rows); $query = "select cid table offset random() * (select count(*) table) limit 1"; $query = "select * table order rand() limit 1"; 


give null result in mysql_query($query).

higher php code obtain row same table ok specifying where, don't understand why can't retrieve random one.

here code snippet:

    $query = "select uid,clu uable 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 * table uid = $uid , 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 * fable 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 * 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 table limit 1 offset ".rand(1,$num_rows);     $query = "select cid table offset random() * (select count(*) table) limit 1"; //        $query = "select * table order 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 * table 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 ok, think you're starting @ wrong place. when you're uncertain how frame in sql, open sql client sequelpro or navicat , try writing few queries hand until result want. (also gives chance double-check contents of relevant tables , ensure expected data there.) can go php full confidence sql code correct, if there's problem must php (either variables inject mysql statement, or way call statement).


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -