我试图将大约1000条记录归入MySQL。但我的尝试仅在第一个查询上“插入”。之后,我收到'或死'错误消息。我尝试将if语句更改为随机数(除了五个),但它总是在第一次插入后死亡。我做错了什么?
$i=0;
foreach(.....){
if($>5){
insert($name, $phone);
}
}
function insert($n, $p){
$dbhost = "---";
$dbuser = "---";
$dbpass = "---";
$dbname = "---";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to mysql");
mysql_select_db($dbname);
$query ="INSERT INTO foo (name, phone) VALUES ('$n', '$p')";
mysql_query($query) or die('Error, query failed');
mysql_close();
}
答案 0 :(得分:3)
这是你的代码
$i=0;
foreach(.....){
if($>5){
insert($name, $phone);
}
}
function insert($n, $p){
$dbhost = "---";
$dbuser = "---";
$dbpass = "---";
$dbname = "---";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to mysql");
mysql_select_db($dbname);
$query ="INSERT INTO foo (name, phone) VALUES ('$n', '$p')";
mysql_query($query) or die('Error, query failed');
mysql_close();
}
连接到MySQL只是为了插入一行,然后是disconnecting and reconnecting is a VERY EXPENSIVE OPERATION !!!
尝试重新组织它:
$dbhost = "---";
$dbuser = "---";
$dbpass = "---";
$dbname = "---";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to mysql");
mysql_select_db($dbname);
$i=0;
foreach(.....){
if($>5){
insert($name, $phone);
}
}
mysql_close();
function insert($n, $p){
$query ="INSERT INTO foo (name, phone) VALUES ('$n', '$p')";
mysql_query($query) or die('Error, query failed');
}
答案 1 :(得分:2)
您是否收到主键错误?