php的mysqli模块相关操作记录
时间:2010-10-25 来源:神龙升空
//创建一个连接
$conn = mysqli_connect("domain","uid","password","dbName");
if (mysqli_connect_errno())
exit();
$conn_local = mysqli_connect("domain","uid","password","dbName");
if (mysqli_connect_errno())
exit();
//设置为utf8编码格式
mysqli_query($conn,"set names 'utf8'");
mysqli_query($conn_local,"set names 'utf8'");
$str_sql="select * from table_name order by id limit 100";
$str_insert = "insert into table_other(id, name, age) values(?,?,?)";
//执行查询语句
$rowArray = mysqli_query($conn,$str_sql);
while($row=mysqli_fetch_array($rowArray))
{
$id = $row[0];
$name = $row[1];
$age = $row[2];
//用mysqli_prepare执行插入操作有效避免SQL注入式攻击
if($stmt=mysqli_prepare($conn_local,$str_insert))
{
//绑定参数--第二个参数可选 i(int) d(double) s(string) b(corresponding variable is a blob and will be send in packages)
mysqli_stmt_bind_param($stmt, "sss", $id, $name , $age );
//执行
mysqli_stmt_execute($stmt);
if(mysqli_errno($conn)==0||mysqli_errno($conn)=='1062')
$i++;
else
$str_error .= "insert into table_other(id,name,age) values($id,$name,$age)";
}
}
//关闭连接
mysqli_close($conn);
mysqli_close($conn_local);
echo $str_error;