在php中连接数据库 pdo

时间:2023-12-27 16:34:37

在php中连接数据库 pdo

//数据库信息
$mysql_conf = array(
'host' => '127.0.0.1',
'db' => 'meteorolog_foreign',
'db_user' => 'root',
'db_pwd' => '123456',
);
try {
//创建一个pdo对象
$pdo = new PDO("mysql:host=" . $mysql_conf['host'] . ";dbname=" . $mysql_conf['db'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);
//设置编码
$pdo->exec('set names utf8');
$sql1 = "INSERT INTO me_ir_api_log (name,method,param,server,client,create_time,start_time) VALUES";
$sql1 .= "('$name','$method','$param_json','$server','$client','$create_time','$start_time')";
$res1 = $pdo->exec($sql1);
}catch (PDOException $e){
die('操作失败'.$e->getMessage());
}