<?php
$dsn = 'mysql:host=localhost;dbname=new_schema;';
$user = 'root';
$passworld = '123456';
$id = 4;
$name = "yudeshui";
$age = 12;
try
{
$dbh = new PDO($dsn,$user,$passworld);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); //禁用prepared statements的仿真效果
$dbh->exec("set names 'utf8'");
echo "连接成功".'</br>';
//插入一条数据
//$sql = "INSERT INTO user(id,NAME,age) VALUES (:id,:NAME ,:age)";
//更新一条书数据
$sql = "UPDATE user SET NAME = :NAME, age = :age WHERE id = :id";
//从数据库中选择数据,并将结果赋予一个变量,testtable为数据库表
$stmt=$dbh->prepare($sql);
$stmt->bindParam(':id',$id);
$stmt->bindParam(':NAME',$name);
$stmt->bindParam(':age',$age);
$stmt->execute();
}
catch(PDOException $e){
echo 'PDO Exception Caught';
echo 'Error with the database:<br />';
echo 'SQL Query:',$sql;
echo 'Connection failed:'.$e->getMessage();
}
?>
PHP数据库的简单操作
最新推荐文章于 2024-08-10 08:41:39 发布