将'print rand'插入DB

时间:2014-02-01 14:34:02

标签: php mysql database random

我正在尝试将一个随机的8位数字插入数据库

这是我用来生成随机数的代码

'insert 'print rand' into DB '

这是我当前的插入代码

     mysql_select_db("cl49-XXX", $con); $sql="
   INSERT INTO member (title,firstname,lastname,daytimetel,mob,work,email,
   address1,address2,towncity,postcode,country,password,phrase,businesstype,businesscontact,regno,regcountry,pos,created) 

  VALUES('$title','$firstname','$lastname','$daytimetel','$mob','$work','$email',
   '$address1','$address2','$towncity','$postcode','$country','$password','$phrase','$businesstype','$businesscontact','$regno','$regcountry','$pos','$created')";

我该如何插入?

1 个答案:

答案 0 :(得分:2)

要获取号码,请使用mt_rand()

$number = mt_rand( 10000000, 99999999);

要将其存储在数据库中,请将其插入PDO

$pdo = new PDO( '', $db_username, $db_password); // read the docs for this
$stmt = $pdo->prepare('INSERT INTO mytable (column_name) VALUES ( ? )');
$stmt->execute( array( $number));

这是您的一般查询

   INSERT INTO your_table (your_column_name) VALUES ( '$number' )