希望大侠,把整个程序写完整了,我不会php,呵呵
mysql 数据库 localhost,密码 123456 ,stu数据库中的表是xuesheng(name varhcar(6),id int,sex char,age smallint)
我希望,往xuesheng 这个表中,循环插入 10000 条记录,数据用random() 函数产生。。。。 或者,发到我邮箱 1171851914@qq.com
求:循环往mysql 数据库插入10000条记录的php代码
答案:2 悬赏:60 手机版
解决时间 2021-02-14 10:00
- 提问者网友:爱了却不能说
- 2021-02-13 16:22
最佳答案
- 五星知识达人网友:白昼之月
- 2021-02-13 16:50
SQL:
CREATE DATABASE `stu` ;
USE `stu`;
DROP TABLE IF EXISTS `xuesheng`;
CREATE TABLE `xuesheng` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(6) NOT NULL,
`sex` varchar(6) NOT NULL,
`age` smallint(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CODE:
$config = array();
$config['host'] = 'localhost';
$config['user'] = 'root';
$config['dbpass'] = 'root';
$config['dbname'] = 'stu';
$con = mysql_connect($config['host'], $config['user'], $config['dbpass']);
mysql_query("set names 'UTF8'");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
mysql_select_db($config['dbname']);
for ($i=1; $i<=10000; $i++)
{
insert();
}
}
function insert()
{
$name = 'name'.rand();
$sex = array("男","女");
$rand_sex = $sex[array_rand($sex,1)];
$age = rand(10,20);
$sql = "INSERT INTO xuesheng (name, sex, age) VALUES ('".$name."', '".$rand_sex."', ".$age.")";
mysql_query($sql);
}
CREATE DATABASE `stu` ;
USE `stu`;
DROP TABLE IF EXISTS `xuesheng`;
CREATE TABLE `xuesheng` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(6) NOT NULL,
`sex` varchar(6) NOT NULL,
`age` smallint(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CODE:
$config = array();
$config['host'] = 'localhost';
$config['user'] = 'root';
$config['dbpass'] = 'root';
$config['dbname'] = 'stu';
$con = mysql_connect($config['host'], $config['user'], $config['dbpass']);
mysql_query("set names 'UTF8'");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
mysql_select_db($config['dbname']);
for ($i=1; $i<=10000; $i++)
{
insert();
}
}
function insert()
{
$name = 'name'.rand();
$sex = array("男","女");
$rand_sex = $sex[array_rand($sex,1)];
$age = rand(10,20);
$sql = "INSERT INTO xuesheng (name, sex, age) VALUES ('".$name."', '".$rand_sex."', ".$age.")";
mysql_query($sql);
}
全部回答
- 1楼网友:鸠书
- 2021-02-13 18:25
conn.php ->数据库连接文件
取出数据:
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯