Helpful Information
 
 
Category: MySQL and other databases
random sql

how to randomize unique id from table? :p

i have id, path, file as field names :D

i want something like

<option value="path/file">random id</option>

I don't quite understand. Do you want to select a random existing ID from the table, or create a new one?

Do you want to select a random existing ID from the table

sorry :D existing random id from table :p

$rs = mysql_query('select id from tablename;');
$randomId = array();
while($row = mysql_fetch_array($rs))
array_push($randomId, $row['id']);
$randomId = $randomId[rand(0, count($randomId))];

sorry :( but where will i put it? :p


<?php
$result = mysql_query($qStr);
$NumRows = mysql_num_rows($result);
?><body bgcolor="black" text="white" alink="green" vlink="red" link="blue">
<form name="dynamicselector">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="178">
<tr>
<td width="35%" valign="top" align="left">
<select name="dynamicselector2" size="<?= strstr($_SERVER["HTTP_USER_AGENT"], "MSIE") != false ? "1" : "2" ?>" onChange="generateimage(this.options[this.selectedIndex].value)">
<?php
while( $row = mysql_fetch_array($result))
{
echo "<option value=\"{$row[1]}/{$row[2]}\">{$row[0]}</option>\n";
}// $row[0] here is where random id part should go
?>
</select>
</td>
<td width="65%" valign="top" align="left"><ilayer id="dynamic1" width=100% height=178><layer id="dynamic2" width=100% height=178><div id="dynamic3"></div></layer></ilayer></td>
</tr>
</table>
</form>

<script>

//Dynamic Image selector Script- © Dynamic Drive (www.dynamicdrive.com)
//For full source code, installation instructions,
//100's more DHTML scripts, visit dynamicdrive.com

var ie4=document.all
var ns6=document.getElementById
var tempobj=document.dynamicselector.dynamicselector2
if (ie4||ns6)
var contentobj=document.getElementById? document.getElementById("dynamic3"): document.all.dynamic3
function generateimage(which){
if (ie4||ns6){
contentobj.innerHTML='<center>Loading image...</center>'
contentobj.innerHTML='<center><img src="'+which+'"><br><br></center>'
}
else if (document.layers){
document.dynamic1.document.dynamic2.document.write('<center><img src="'+which+'"><br><br></center>')
document.dynamic1.document.dynamic2.document.close()
}
else
alert('You need NS 4+ or IE 4+ to view the images!')
}

function generatedefault(){
generateimage(tempobj.options[tempobj.options.selectedIndex].value)
}

if (ie4||ns6||document.layers){
if (tempobj.options.selectedIndex!=-1){
if (ns6)
generatedefault()
else
window.onload=generatedefault
}
}
</script><?php mysql_close($svrConn);?>

Ah, I see. You want to just order the results randomly? Try adding ORDER BY RAND() to the end of your query.

okies :p thx :D

how do i now return results randomly in PostgreSQL? :p tried adding ORDER BY RAND() to the end of the query. returns an error "function rand() does not exist"

any ideas? :D

In PostgreSQL the function is called RANDOM().










privacy (GDPR)