简体   繁体   English

生成100个10位随机数并保存到数据库

[英]generate 100 10-digit random numbers and save to database

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. 我正在编写一个脚本以生成100个10位随机数,但我仍然遇到此错误:“在第1行中'rand(1111111111, 9999999999)'检查与您的MySQL服务器版本相对应的手册以在'rand(1111111111, 9999999999)'附近使用正确的语法。

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. MySQL抱怨的语法错误(在您的SQL语句中)缺少值列表周围的括号。 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. 在示例代码中, 'rand(11111,99999)'是一个文字字符串。 It's not a reference to a function. 它不是对函数的引用。


If we want to use the MySQL RAND() function, we could do something like this: 如果要使用MySQL RAND()函数,可以执行以下操作:

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. 我们可以执行100次,或者编写返回100行并在单个插入中插入100行的SELECT语句会更有效。

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. 就数据库性能而言,运行单个插入100行的INSERT语句通常比执行100个单独的插入语句更有效。 Processing RBAR (row by agonizing row) can be excruciatingly slow. 处理RBAR(行排成行)可能会非常慢。

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. 如果需要循环,如果我们一次插入四行,则只需要循环25次。 And that's 75% fewer statements we need to execute. 这比我们需要执行的语句少75%。

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文本,然后重复执行相同的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: 您也可以使用MySQL直接执行以下操作:

INSERT INTO random (random) 
SELECT CAST( (RAND()*(9999999999-1111111111)) AS UNSIGNED)  + 1111111111;

or for 100 rows: 或100行:

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();
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM