This is a pretty simple function yet it keeps giving me errors. Im writing a script to generate 100 10-digit random numbers but I keep having this error: "check the manual that corresponds to your MySQL server version for the right syntax to use near 'rand(1111111111, 9999999999)'
at line 1.
This is my code
<?php
$cxn = new mysqli("localhost", "CJroot", "password", "random");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
for ($i=0; $i < 100; $i++) {
$sql = "INSERT INTO random (random) VALUES 'rand(1111111111, 9999999999)'";
if(!mysqli_query($cxn,$sql)) {
die("Error: ".mysqli_error($cxn));
}
}
/* close connection */
$cxn->close();
?>
您需要像这样连接字符串。
$sql = "INSERT INTO random (random) VALUES '" . rand(1111111111, 9999999999) . "'";
The syntax error that MySQL is complaining about (in your SQL statement) is missing parens around the values list. This is invalid:
INSERT INTO ... VALUES 'foo'
Should be
INSERT INTO ... VALUES ( 'foo' )
^ ^
Once you get over that hump, the value enclosed in single quotes is interpreted as a string literal . In the example code, 'rand(11111,99999)'
is a literal, a string of characters. It's not a reference to a function.
If we want to use the MySQL RAND()
function, we could do something like this:
INSERT INTO random (random) VALUES ( FLOOR(1000000000 + RAND() * 9000000000) )
We can execute that 100 times, or, it would be more efficient to write SELECT statement that returns 100 rows, and insert 100 rows in a single insert.
Or:
INSERT INTO random (random) VALUES
( FLOOR(1000000000 + RAND() * 9000000000) )
,( FLOOR(1000000000 + RAND() * 9000000000) )
,( FLOOR(1000000000 + RAND() * 9000000000) )
, ...
,( FLOOR(1000000000 + RAND() * 9000000000) )
In terms of database performance, it is usually more efficient to run a single INSERT statement that inserts 100 rows, than it is to execute 100 individual insert statements. Processing RBAR (row by agonizing row) can be excruciatingly slow.
If there's a need for a loop, if we insert four rows at time, we need only need to go through the loop 25 times. And that's 75% fewer statements we need to execute.
We can specify the SQL text to be executed just one time, outside of the loop, and then just repeatedly execute that same SQL
$sql = "INSERT INTO random (random) VALUES"
. " ( FLOOR(1000000000 + RAND() * 9000000000) )"
. ",( FLOOR(1000000000 + RAND() * 9000000000) )"
. ",( FLOOR(1000000000 + RAND() * 9000000000) )"
. ",( FLOOR(1000000000 + RAND() * 9000000000) )";
for ($i=0; $i < 25; $i++) {
if(!mysqli_query($cxn,$sql)) {
die("Error: ".mysqli_error($cxn));
}
}
you can also do it direct with MySQL like this:
INSERT INTO random (random)
SELECT CAST( (RAND()*(9999999999-1111111111)) AS UNSIGNED) + 1111111111;
or for 100 rows:
INSERT INTO random (random)
SELECT CAST( (RAND()*(9999999999-1111111111)) AS UNSIGNED) + 1111111111 AS random FROM (
SELECT 0 AS h UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) h
CROSS JOIN
( SELECT 0 AS l UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) l;
This works fine
<?php
$cxn = new mysqli("localhost", "CJroot", "password", "random");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql = "INSERT INTO random (random) VALUES"
. " ( FLOOR(1000000000 + RAND() * 9000000000) )"
. ",( FLOOR(1000000000 + RAND() * 9000000000) )"
. ",( FLOOR(1000000000 + RAND() * 9000000000) )"
. ",( FLOOR(1000000000 + RAND() * 9000000000) )";
for ($i=0; $i < 25; $i++) {
if(!mysqli_query($cxn,$sql)) {
die("Error: ".mysqli_error($cxn));
}
}
$cxn->close();
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.