felito Posted April 19, 2011 Share Posted April 19, 2011 hi. for debug test, i need store 200 random numbers in the database I obviously want to avoid insert one by one how i can do that? (mysql) i discover this, but is not what i pretend. UPDATE tablename SET column1 = (rand() * 50) Link to comment https://forums.phpfreaks.com/topic/234111-add-random-data-to-the-table/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 19, 2011 Share Posted April 19, 2011 <?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... ?> Link to comment https://forums.phpfreaks.com/topic/234111-add-random-data-to-the-table/#findComment-1203283 Share on other sites More sharing options...
felito Posted April 19, 2011 Author Share Posted April 19, 2011 you forget the mysql_query Yes it works, but i believe that can be done with a line of code in a mysql way thanks! Link to comment https://forums.phpfreaks.com/topic/234111-add-random-data-to-the-table/#findComment-1203291 Share on other sites More sharing options...
kickstart Posted April 19, 2011 Share Posted April 19, 2011 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 Link to comment https://forums.phpfreaks.com/topic/234111-add-random-data-to-the-table/#findComment-1203375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.