Blogger Information
Blog 37
fans 0
comment 0
visits 20789
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP编程第九课:php基础9-PHP培训九期线上班
渡劫小能手
Original
691 people have browsed it

一、pdo操作:连接、增删查改操作(手写)

1、connect


实例

<?php
# connect.php文件
// 数据库连接参数
$db = [
    'type' => 'mysql',
    'host' => 'localhost',
    'dbname' => 'movie',
    'username' => 'root',
    'password' => 'root',
    'port' => 3306
];

// {}在双引号里,把变量包裹起来的符号,它本身没有意义。因为在""中出现变量,双引号不知道
//你是让它等于变量,还是普通字符串
$dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}";

// 连接数据库
try {
    $pdo = new PDO($dsn, $db['username'], $db['password']);
} catch (PDOException $e) {
    die('错误信息: ' . $e->getMessage());
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

2、insert


实例

<?php
require __DIR__ . '/connect.php';
$sql = 'INSERT INTO `category` SET `name`=:n ,`alias`=:a ';
$stmt = $pdo->prepare($sql);
//print_r($stmt);
$name = 'xby';
$alias = '西班牙';
$stmt->bindParam('n', $name, PDO::PARAM_STR);
$stmt->bindParam('a', $alias, PDO::PARAM_STR);
//print_r($stmt);
$add = $stmt->execute();
if ($add) {
    $count = $stmt->rowCount();
    if ($count > 0) {
        echo '数据插入成功';
    } else {
        echo '数据插入失败';
    }
} else {
    die('<pre>' . print_r($stmt->errorInfo(), true));
}
$pdo = null;

运行实例 »

点击 "运行实例" 按钮查看在线实例

3、delete


实例

<?php
require __DIR__ . '/connect.php';
$sql = 'DELETE FROM `category` WHERE `cate_id`=:id ';
$stmt = $pdo->prepare($sql);
//print_r($stmt);
$id = 8;
$stmt->bindParam('id', $id, PDO::PARAM_INT);
//print_r($stmt);
$add = $stmt->execute();
if ($add) {
    $count = $stmt->rowCount();
    if ($count > 0) {
        echo '数据删除成功';
    } else {
        echo '数据删除失败';
    }
} else {
    die('<pre>' . print_r($stmt->errorInfo(), true));
}
$pdo = null;

运行实例 »

点击 "运行实例" 按钮查看在线实例

4、update

实例

<?php
require __DIR__ . '/connect.php';
$sql = 'UPDATE `category` SET `name`=:n ,`alias`=:a WHERE `cate_id`=:id ';
$stmt = $pdo->prepare($sql);
//print_r($stmt);
$id = 8;
$name = 'adly';
$alias = '澳大利亚';
$stmt->bindParam('n', $name, PDO::PARAM_STR);
$stmt->bindParam('a', $alias, PDO::PARAM_STR);
$stmt->bindParam('id', $id, PDO::PARAM_INT);
//print_r($stmt);
$add = $stmt->execute();
if ($add) {
    $count = $stmt->rowCount();
    if ($count > 0) {
        echo '数据修改成功';
    } else {
        echo '数据修改失败';
    }
} else {
    die('<pre>' . print_r($stmt->errorInfo(), true));
}
$pdo = null;

运行实例 »

点击 "运行实例" 按钮查看在线实例

5、select


实例

<?php
require __DIR__ . '/connect.php';
$sql = 'SELECT * FROM `category`';
$stmt = $pdo->prepare($sql);
//print_r($stmt);
$add = $stmt->execute();
if ($add) {
    $arr = $stmt->fetchAll();
    foreach ($arr as $v){
        echo $v['cate_id'].'---'.$v['name'].'---'.$v['alias'].'---'.$v['status'].'<hr/>';
    }
} else {
    die('<pre>' . print_r($stmt->errorInfo(), true));
}
$pdo = null;

运行实例 »

点击 "运行实例" 按钮查看在线实例

2019-11-25_001110.jpg

2019-11-25_000908.jpg

2019-11-25_000934.jpg

2019-11-25_000942.jpg

2019-11-25_000949.jpg

Correcting teacher:查无此人查无此人

Correction status:qualified

Teacher's comments:完成的不错,继续加油。
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!