[PHP] how can i make this pick a random line in the DB?

Brad

Well-Known Member
Jun 5, 2012
2,320
993
Ok so I've created a automatic feature like lottery were it should pick a random ticket number or username but seems not to show anything
PHP:
<?php
$n1 = dbquery("SELECT * FROM lottery_tickets WHERE username = '" . TICKET . "'"); ?>
<?php
if(isset($_POST['pick']))
{
    $N1 = rand('" . TICKET . "');
  
  
    $WINNER = mysql_fetch_assoc(dbquery("SELECT username FROM lottery_tickets WHERE username = '". $N1 ."'"));
    if($WINNER['username'] == ""){ $WINNER['username'] = "NONE"; }
  
    fMessage('ok', 'Winner: <b>'. $WINNER['username'] .'</b> ('. $N1 .')');
}  ?>

thanks in advance.
 

IntactDev

Member
Nov 22, 2012
399
71
PHP:
<?php
$n1 = dbquery("SELECT * FROM lottery_tickets WHERE username = '" . TICKET . "' ORDER BY RAND();"); ?>
<?php
if(isset($_POST['pick']))
{
    $WINNER = mysql_fetch_assoc(dbquery("SELECT username FROM lottery_tickets WHERE username = '". $n1 ."' ORDER BY RAND();"));
    if($WINNER['username'] == ""){ $WINNER['username'] = "NONE"; }
 
    fMessage('ok', 'Winner: <b>'. $WINNER['username'] .'</b> ('. $N1 .')');
}  ?>

The code makes no sense to me, but I basically added "ORDER BY RAND();" within the query. Also, take a look at MySQLi or PDO:
 

Brad

Well-Known Member
Jun 5, 2012
2,320
993
PHP:
<?php
$n1 = dbquery("SELECT * FROM lottery_tickets WHERE username = '" . TICKET . "' ORDER BY RAND();"); ?>
<?php
if(isset($_POST['pick']))
{
    $WINNER = mysql_fetch_assoc(dbquery("SELECT username FROM lottery_tickets WHERE username = '". $n1 ."' ORDER BY RAND();"));
    if($WINNER['username'] == ""){ $WINNER['username'] = "NONE"; }

    fMessage('ok', 'Winner: <b>'. $WINNER['username'] .'</b> ('. $N1 .')');
}  ?>

The code makes no sense to me, but I basically added "ORDER BY RAND();" within the query. Also, take a look at MySQLi or PDO:

I'll explain a user buys a lottery ticket, then it stores their usersname and ticket number in the database, I want that to pick a random winner :p thanks ill take a look :)
 

Brad

Well-Known Member
Jun 5, 2012
2,320
993
Heres a preview

And here is the full code

PHP:
<?php
$n1 = dbquery("SELECT * FROM lottery_tickets WHERE username = '" . TICKET . "'"); ?>
<?php


$getSettings = dbquery("SELECT * FROM users WHERE username = '" . USER_NAME . "'");
$userAccount = mysql_fetch_assoc($getSettings);
?>
<?php

if (!defined('IN_HK') || !IN_HK)
{
    exit;
}

if (!HK_LOGGED_IN || !$users->hasFuse(USER_ID, 'fuse_housekeeping_moderation'))
{
    exit;
}

if(isset($_POST['pick']))
{
    $N1 = rand('" . TICKET . "');
   
   
    $WINNER = mysql_fetch_assoc(dbquery("SELECT username FROM lottery_tickets WHERE username = '". $N1 ."'"));
    if($WINNER['username'] == ""){ $WINNER['username'] = "NONE"; }
   
    fMessage('ok', 'Winner: <b>'. $WINNER['username'] .'</b> ('. $N1 .')');
}

require_once "top.php";   

$countEnter = mysql_num_rows(dbquery("SELECT null FROM lottery_tickets"));       

echo '<h1>Lotto System Records ('. $countEnter .' Entries) - ('. $countEnter * 1 .' tokens Earned) - ('. $countEnter * 1 * 2 .' Tokens Prize)</h1>';

?>
<p>This tool allows you to view all the user enteries for the lotto system.</p>
<br>
<form method="post">
    <center><input type="submit" name="pick" value="Select Random Winner"></center>
</form>
<br><br>
<table align="center">
<tr><td align="center" width="150"><b>Username</b></td><td align="center"><b>Numbers</b></td><td align="center" width="300"><b>Date Entered</b></td></tr>
<?php

$query = dbquery("SELECT username,ticket_number FROM lottery_tickets");

while($assoc = mysql_fetch_assoc($query))
{
    echo '<tr><td align="center">'. $assoc['username'] .'</td><td align="center"><b>'. $assoc['ticket_number'] .'</b></td><td align="center">'. date('r', $assoc['date']) .'</td></tr>';
}

?>
</table>
 

Khalil

IDK
Dec 6, 2011
1,642
786
All I could think of at the moment is:

PHP:
$gQuery = mysql_query("SELECT * FROM `lottery_tickets` ORDER BY RAND() LIMIT 1") or die(mysql_error());

while($Info = mysql_fetch_array($gQuery)) { print("The winner is: ".$Info["username"]."."); }
 

Users who are viewing this thread

Top