Jump to content

add random data to the table


felito

Recommended Posts

<?php
$num = 200;
$arr = array();
while(count($arr) < $num){
$rand = rand();
if(!in_array($rand,$arr)){
	$arr[] = $rand;
}
}

$data = implode('),(',$arr);
$query = "INSERT INTO your_table (your_column) values ($data)";
echo $query;

//execute your query here...

?>

Hi

 

This will do it for you:-

 

INSERT INTO testrandom
(`aRandomNumber`)
SELECT (rand() * 50)
FROM (SELECT a.i +b.i*10 + c.i*100 AS  someinteger
FROM integers a,integers b,integers c
HAVING someinteger < 200) fred

 

You need a table called integers with an integer column called i, with 10 rows. i has values from 0 to 9 in those rows. This allows you to generate a number, and here is used to generate all the numbers between 0 and 999, with any greater than or equal 2 disposed of by the HAVING clause. This gives you 200 rows. The select then creates a random number for each row and that is inserted into the column named in the INSERT query (this is assuming an autonumber primary key).

 

All the best

 

Keith

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.