search
HomeheadlinesNew operation of pdo in php

* PDO preprocessing mainly uses the PDOStatement object

* This object is created through: $pdo->prepare() method

* Both read and write operations can be done through: $pdoStmt- >execute() method

* In addition to using the ? symbol, placeholders in preprocessed SQL statements can also use named parameters, such as: name:email...

* The following is an example of adding new data for demonstration. It is divided into 6 steps:

* 1. Connect to the database and create a PDO object

* 2. Prepare preprocessed SQL statements and placeholders Use named parameter format:

* 3. Create PDO preprocessing object: instance of PDOStatement

* 4. Bind parameters to SQL statement object, preprocessing object

* 5. Perform new operations (in PDO, use execute() for reading and writing)

* 6. Destroy PDO objects (optional)

//1. Connect to the database and create PDO Object

$pdo = new PDO('mysql:dbname=php', 'root', 'root');

//2. Prepare the preprocessing SQL statement. The placeholder uses the named parameter format:

$sql = "INSERT `user` SET `user_name`=:name, `email`=:email,`password`=sha1(:password)";

//3. Create the PDO preprocessing object

$stmt = $pdo->prepare($sql);

/ /View the generated SQL statement, which can be copied to the SQL command window and run to ensure the correctness of the statement

echo $stmt->queryString;exit();

//4. Bind parameters to the preprocessing object

$data = ['name'=>'杨过','email'=>'yg@php.cn', 'password'=>'123'];
$stmt->bindParam(':name',$data['name'],PDO::PARAM_STR);
$stmt->bindParam(':email',$data['email'],PDO::PARAM_STR);
$stmt->bindParam(':password',$data['password'],PDO::PARAM_STR);

//5. Perform new operations

if($stmt->execute()){
    //rowCount():返回上一个SQL语句影响的行数
    echo &#39;<h3 id="成功添加了-stmt-rowCount-条记录">成功添加了&#39;.$stmt->rowCount().&#39;条记录</h3>&#39;;
} else {
    echo &#39;<h3 id="添加失败">添加失败</h3>&#39;;
    print_r($stmt->errorInfo());
    exit();
}

//6. Destroy PDO objects

$pdo = null;
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function