Home  >  Article  >  Backend Development  >  How to implement PDO-based preprocessing in PHP

How to implement PDO-based preprocessing in PHP

墨辰丷
墨辰丷Original
2018-05-24 14:26:171482browse

This article mainly introduces the implementation of PDO-based preprocessing in PHP, and analyzes the relevant operating techniques and precautions in PDO implementation of PDO preprocessing in the form of examples. Friends in need can refer to the following

Examples of this article Use PDO to implement PDO-based preprocessing. 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;

The above is the entire content of this article, I hope it will be helpful to everyone’s study .


Related recommendations:

About PHP MySQL PreprocessingRelevant knowledge of statements

babel-loader filePreprocessorWhat are the methods of using

babel-loader filePreprocessorDetailed explanation of device usage

The above is the detailed content of How to implement PDO-based preprocessing in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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