$servername = "localhost"; //服务器
$username = "root"; //用户名
$password = "root"; //密码
$dbname="base"; //表名
$params = array (
PDO::MYSQL_ATTR_INIT_COMMAND => ‘SET NAMES \‘UTF8\‘‘ , //设置字符集 保证中文不乱码
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //设置当前pdo的错误处理方式
);
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password, $params);
echo "连接成功";
//查询
$sqlinquire = ‘select * from shop‘;
// $sqlinquire = ‘select * from shop WHERE Id=3||typeId=1‘;//给查询加入限制条件
$preinquire = $conn->prepare($sqlinquire);
$preinquire->execute();
$resultinquire=$preinquire->fetchAll(PDO::FETCH_ASSOC);
//插入,增加记录
$str=‘‘;
$arr=array();
for($i=0;$i<10;$i++){
if($i==0){
$str="(?,?,?)";
}else{
$str.=",(?,?,?)";
}
$s="张三".rand(1000,100000);
$t=rand(1,3);
$m=‘¥‘.rand(100,1000);
$arr[]=$s;
$arr[]=$t;
$arr[]=$m;
}
echo $str;
print_r($arr);
$sqladd="INSERT INTO shop(shopname,typeId,money) VALUES $str";
$preadd = $conn->prepare($sqladd);
$preadd->execute($arr);
//修改编辑数据
$sqlchange="UPDATE shop SET money=‘¥1599‘ WHERE Id=2";
$prechange = $conn->prepare($sqlchange);
$prechange->execute();
//删除
$sqldel="DELETE FROM shop";
$predel = $conn->prepare($sqldel);
$predel->execute();
} catch(PDOException $e) {
echo $e->getMessage();
}
原文:https://www.cnblogs.com/zzg02/p/9332918.html