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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.