View Single Post
  #1 (permalink)  
Old 11-06-03, 04:27 PM
dsumpter dsumpter is offline
Newbie Coder
 
Join Date: Jul 2003
Location: Huntington Beach, CA
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
picking random entries with a filter... Double selection problem

I am trying to set up my script to select a random entry for each clientID in my table. For some reason on the clientID's that have more entries I am getting a double or triple return. I think it has something to do with the client side timing out on the script while the sql statement is trying to run, but I am not sure. Does anyone have any advise? Below is my script. Thanks in advance!!!

<?



/* MySQL details */

$dbhost = "localhost";
$dbname = "aci";
$dbuser = "me";
$dbpass = "mypass";

### Default Connection String ###
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Unable to connect to MySQL server." . mysql_error());
$db = mysql_select_db($dbname, $link) or die ("Unable to select database: " . mysql_error());

### Further MySQL Info ###
// Name of the table you wish to get random items out of
$randtable = "target";

### Values you do NOT want included in the random drawing ###
// Turns this function off or on
$xvals = "on";
// set Array of values you do not want included - separated by commas
$xarrsql = "SELECT * FROM monthlyWinner WHERE mwID > '0'";

$xarrResult = @mysql_query($xarrsql,$link) or die("Couldn't Execute xarrsql query.");

while ($row = mysql_fetch_array($xarrResult)) {
$targetIDx = $row['targetID'];

$xarrBlock .= "\"$targetIDx\", ";

}

$xarr = array("$xarrBlock");

//print ("$xarrBlock <br>");

// id field to pick at random
$idvar = "targetID";



//get client table info

$sql = "SELECT clientID, clientName FROM client WHERE clientID > '0' ORDER BY clientName";

$clientResult = @mysql_query($sql,$link) or die("Couldn't Execute query.");

while ($row = mysql_fetch_array($clientResult)) {

$clientIDm = $row['clientID'];
$clientName = $row['clientName'];


//Start random for client 1

// Make a query to draw the records from the table
$randsql_result = mysql_query("SELECT targetID, clientID FROM $randtable WHERE clientID = $clientIDm",$link) or die ('Database Query Failed.
Please check the $randtable variable under Further MySQL Info, and make sure you have the correct table name.');

if(mysql_num_rows($randsql_result) == 0) {
print "- <b><i>$clientName</b></i> had no valid entries in the table, so no monthly winner was sent <br>";

//mysql_free_result($randsql_result);

}

if(mysql_num_rows($randsql_result) != 0) {

// Load entries into an array to select one randomly
$i = 0;
while($row = mysql_fetch_row($randsql_result)) {
if($xvals == "on") {
if(!in_array("$row[0]", $xarr) && $row[0] != "") {
$rand[$i] = $row[0];
$i++;
}
} elseif($row[0] != "") {
$rand[$i] = $row[0];
$i++;
}
}
mysql_free_result($randsql_result);

// Select a random entry from the assembled array
if(count($rand) == "0") { print "There are no entries in the array!"; exit; }
$randnum = array_rand($rand);

$rst = mysql_query("SELECT * FROM $randtable WHERE $idvar = '$rand[$randnum]' LIMIT 1", $link) or die('Could not retrieve random
entry from databse. Please check the $idvar variable under IMPORTANT TABLE INFO to make sure the id field is correct');
// $q = mysql_fetch_array($rst);

while ($row = mysql_fetch_array($rst)) {

$qtargetID = $row['targetID'];
$qtargetEmail = $row['targetEmail'];
$qclientID = $row['clientID'];
}

$qsql = "INSERT INTO monthlyWinner VALUES ('', '00000000000000', '$qtargetID', '$qclientID')";

$qsqlResult = mysql_query($qsql,$link) or die("Couldn't update monthlyWinner with query.");

// this is where the email info will go

print "<b>- $clientName:</b> targetID: $qtargetID, targetEmail: $qtargetEmail <i>SENT</i><br>";

mysql_free_result($rst);

}

}
?>
__________________
Dan
Reply With Quote