php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22597 PHP can't select random rows from MySql
Submitted: 2003-03-07 18:42 UTC Modified: 2003-03-08 05:27 UTC
From: grugli at mmedia dot is Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 4.3.1 OS: Windows XP
Private report: No CVE-ID: None
 [2003-03-07 18:42 UTC] grugli at mmedia dot is
This MySql-query doesn't seem to work.
$Query = "select * from $TableName ORDER BY RAND() LIMIT 1";

The only thing that the query returns is the first row and nothing else.
ORDER BY RAND() works very well from the mysql command line but does not work right through PHP.

The same bug was reported in september 2001 (Bug #13287) and was closed by derick@php.net becuse he thought it was a mysql-bug

I know this is not a MySql-bug. It must be a bug in PHP becuse the mysql-command line works fine.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-08 05:27 UTC] georg@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

PHP uses the same api call (mysql_query/mysql_real_query) as in mysql command line client and it doesn't change the query string.
 [2003-03-12 20:45 UTC] genius at airmail dot net
The bug is in mysql as I can reproduce it from the command-line. It also may be windows-specific ... this bug is often mistaken for not providing a random seed i.e. rand(now()) but the problem persists even with the seed.
 [2003-03-12 21:12 UTC] genius at airmail dot net
I made the previous comment too quickly, it appears to me that this indeed is a php (for win32 .. cause it tested on linux w/o problems) problem.

when running mysql command line this command 'select * from pictures order by rand()' produces random results, however when calling that same command from php it always returns the same result.

in my previous post i was running 'select * from pictures order by rand(now())' from the command line which returns the same result every time, which lead me to believe the bug was in mysql.

someone please reopen this bug report
 [2003-03-17 09:52 UTC] gaui at gaui dot is
Ok, this problem persists here and is VERY annoying.

Please fix this.
 [2003-04-29 05:06 UTC] mir at irczone dot dk
I have this problem too, using PHP 4.3.0 on Mac OS X 
10.2.5 and also on FreeBSD 4.7 with PHP 4.3.1 both 
running as a module in Apache 1.3.27 and talking with a 
MySQL 3.23.54 server. When using the mysql client on 
the commmandline, the RAND() works fine.

It worked before, with the exact same code, 
unfortunately I don't know which version of PHP broke 
it - my guess would be PHP 4.3.

Hope you can fix this ASAP.
 [2003-07-29 10:57 UTC] justin at visunet dot ie
When you say:

This MySql-query doesn't seem to work.
$Query = "select * from $TableName ORDER BY RAND() LIMIT 1";
The only thing that the query returns is the first row and nothing
else.

1) Limit should be used like so .. LIMIT 0,1
2) In the above example LIMIT makes mySQL returns the first row and nothing else.
 [2004-04-14 20:36 UTC] szii at sziisoft dot com
Reproduced on stock SuSE 8.2 with shipped Apache and using PHP as a module.

server:/web/common # httpd -v   
Server version: Apache/1.3.27 (Linux/SuSE)
Server built:   Oct 29 2003 19:37:53


mysql> SHOW VARIABLES LIKE '%version%';    
+------------------+------------------------------------------------------------+
| Variable_name    | Value                                                      |
+------------------+------------------------------------------------------------+
| bdb_version      | Sleepycat Software: Berkeley DB 3.2.9a: (January 23, 2003) |
| protocol_version | 10                                                         |
| version          | 3.23.55-Max-log                                            |
+------------------+------------------------------------------------------------+
3 rows in set (0.00 sec)

The mysql client (command line) works fine.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 08:01:29 2024 UTC