Jump to content

PHP Random from MYSQL


plznty

Recommended Posts

How would I make it so that

for every set of data where a fieldname is 1+ it picks a random one out.

For example

User            Hit

Luke            0

Peter            1

Alex              3

Peter            1

 

For every value where Hit is 1 or over it selects out of the query results a random username.

I would definitely have a unique id for any table [Edit] when comes to storing users for sure. If you can work that out then the following will be helpful.

 

Take a look at solution 2 as opposed to using rand() within your query.

Before you do that, Google 'mysql why order by rand() is bad'.

 

Did you do this ^^^?

 

Reading some of the results will help you make the decision whether you want to use ORDER BY RAND() or not. There are also better solutions in each explanation of the downfalls.

I'd have to say with that small of a record set, you'll be OK. Just remember that you used ORDER BY RAND() there, and if the query starts to bog down as the table grows, change to a more efficient method.

There's an error in the query syntax (semicolon inside the quotes), and you need to actually do something with the query result before you can use the values it returns. You also had nothing in place to check to see if the query succeeded.

 

$query = "SELECT * FROM users WHERE clicks > 0 ORDER BY RAND() LIMIT 1";
if( $result = mysql_query($query) ) {
     $array = mysql_fetch_assoc($result);
     echo $row['email'];
} else {
     echo '<br>Query: ' . $query . '<br>Produced error: ' . mysql_error() . '<br>';
}

it displays nothing.

This is doing my head in.

Thanks for helping though

<?php
    $connect = mysql_connect("localhost", "dbname", "pass") or die(mysql_error());
    mysql_select_db("db") or die(mysql_error());
$query = "SELECT * FROM users WHERE clicks > 0 ORDER BY RAND() LIMIT 1";
if( $result = mysql_query($query) ) {
     $array = mysql_fetch_assoc($result);
     echo $row['id'];
} else {
     echo '<br>Query: ' . $query . '<br>Produced error: ' . mysql_error() . '<br>';
}
?>

 

Produces nothing

<?php
    $connect = mysql_connect("localhost", "", "") or die(mysql_error());
    mysql_select_db("") or die(mysql_error());
$query = "SELECT * FROM users WHERE clicks > 0 ORDER BY RAND() LIMIT 1";
if( $result = mysql_query($query) ) {
     $array = mysql_fetch_assoc($result);
     echo "hello".$row['id'];
} else {
     echo '<br>Query: ' . $query . '<br>Produced error: ' . mysql_error() . '<br>';
}
?>

 

Does display "hello"

so something to do with $row variable etc

If you put this code on a live production server, you'll probably want to change the error reporting that's in the else{} to something generic like "Sorry, there was a database error." and log the actual error in a logfile instead, but that's another subject.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.