fortnox007 Posted December 30, 2010 Share Posted December 30, 2010 Hi all, I prety often populate a database just to test stuff. So i thought i make a little script which magically populates the database with multiple rows at ones instead of pressing F5 all the time . I am not sure if this is the best way so I would love some advice of an expert. If someone can use it feel free to use it. Changing $rows is all that is needed to blast your database to the moon. <?php include('connector.php');//connection file //make query $query = "INSERT INTO comments (title, comment)VALUES"; //$data = mysqli_query("SELECT * FROM comments") or die(mysql_error()); $rows =24;//set to 1 million to get free cookies and an angry email from your host $comment = "Lorem Ipsum is simply dummy"; for ($i=0;$i<$rows;$i++){ if ($i<($rows-1)){$comma=',';}else{$comma='';}//preventing a comma on last loop which will prolly break query $title = chr(rand(97,122)).'this is a title with a bogus prefix'; $go .= "('$title', '$comment')$comma"; //hoping to get them in this manner ('var1', 'var2') } //populate this monkey mysqli_query($dbc,$query.$go) or die('Connect Error: ' . mysqli_error($dbc).'number'.mysqli_errno($dbc)); ?> Quote Link to comment Share on other sites More sharing options...
shlumph Posted December 30, 2010 Share Posted December 30, 2010 When looping an insert like that, a transaction is usually best practice. mysql_query("START TRANSACTION"); //Insert statements mysql_query("COMMIT"); http://dev.mysql.com/doc/refman/5.0/en/commit.html Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted December 30, 2010 Author Share Posted December 30, 2010 cool i am going to test that out thank you! I hope i can make it in OOphp style. Ones I know how to program that way. ill post it if someone finds it useful. Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted December 30, 2010 Author Share Posted December 30, 2010 I just found a short example here for php, seems pretty awesome certainly the rollback function. omg i learned a lot today -edit:lol forgot to post it: http://articles.techrepublic.com.com/5100-10878_11-6085922.html Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.