I would like to enter some value into database 1000x times.
Example:
<?php
$n = rand(1,30000);
$n2 = $n."20";
for($i=1;$i=1000;$i++){
mysql_query("INSERT INTO reedem(5) VALUES ('".$n2."')");
}
?>
How could I do that correctly?
I would like to enter some value into database 1000x times.
Example:
<?php
$n = rand(1,30000);
$n2 = $n."20";
for($i=1;$i=1000;$i++){
mysql_query("INSERT INTO reedem(5) VALUES ('".$n2."')");
}
?>
How could I do that correctly?
Prepared statements are useful when you need to run the same query multiple times.
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
$stmt = $mysqli->prepare("INSERT INTO redeem (myColumnName) VALUES (?)");
$n = rand(1,30000) . '20';
$stmt->bind_param("i",$n);
foreach(range(1,1000) as $i) {
$stmt->execute();
}
$stmt->close();
if you want to insert 1000 different random values (as opposed to the same random value 1000 times) move the code that defines $n
inside the loop
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
$stmt = $mysqli->prepare("INSERT INTO redeem (myColumnName) VALUES (?)");
$stmt->bind_param("i",$n);
foreach(range(1,1000) as $i) {
$n = rand(1,30000) . '20';
$stmt->execute();
}
$stmt->close();