Home > Backend Development > PHP Tutorial > Detailed explanation of the sample code for PDO-based preprocessing in PHP

Detailed explanation of the sample code for PDO-based preprocessing in PHP

黄舟
Release: 2023-03-07 07:06:01
Original
1044 people have browsed it

This article mainly introduces the php implementation of preprocessing based on PDO, and analyzes the relevant operating skills and precautions for PDO preprocessing in php based on examples. ##, friends who need it can refer to

The example of this article describes the implementation of PDO-based preprocessing in PHP. Share it with everyone for your reference, the details are as follows:

$servername="localhost";
$username="root";
$password="admin";
//$dbname为我的test数据库
$dbname="test";
try{
  $conn=new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
  //设置pdo错误异常处理
  $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  //预处理SQL并绑定参数
  $stmt=$conn->prepare("INSERT INTO user(user_first,user_last,age)VALUES(:user_first,:user_last,:age)");
  $stmt->bindParam(":user_first",$user_first);
  $stmt->bindParam(":user_last", $user_last);
  $stmt->bindParam(":age",$age);
  //要执行的插入记录
  $user_first="xiao";
  $user_last="liang";
  $age=15;
  $stmt->execute();
  //要执行的插入记录
  $user_first="xiao";
  $user_last="cheng";
  $age=23;
  $stmt->execute();
  echo "News record created successfully!";
}catch(PDOException $e){
  echo "Error:".$e->getMessage();
}
$conn=null;
Copy after login

The above is the detailed content of Detailed explanation of the sample code for PDO-based preprocessing in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template