Blogger Information
Blog 38
fans 0
comment 1
visits 30652
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1. MySQLi面向对象实现更新操作 2. PDO实现删除操作(参考更新操作独立完成)
1
Original
887 people have browsed it

数据库面向对象连接

实例

<?php
/**
 * 面向对象的数据库连接
 */

$db_host = '127.0.0.1';
$db_user = 'root';
$db_pass = 'root';
$db_name = 'php';
$db_char = 'utf8';

//连接数据库服务器返回mysqli对象
$mysqli = @ new mysqli($db_host,$db_user,$db_pass);

if($mysqli->connect_errno){
    return false;
}

$mysqli->select_db($db_name);
$mysqli->set_charset($db_char);

运行实例 »

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

面向对象更新


实例

<?php
require "mysqli_connect.php";

$sql = "UPDATE staff SET name=? WHERE staff_id=?;";

$stmt=$mysqli->stmt_init();

if ($stmt->prepare($sql)){
    $data[] = ['name'=>'足力健','staff_id'=>45];

    $stmt->bind_param('si',$name,$staff_id);

//    $name = '液体钙';
//    $staff_id = 48;
    foreach ($data as $staff){
        $name = $staff['name'];
        $staff_id = $staff['staff_id'];

        $stmt->execute();
    }




}else{
    return false;
}

运行实例 »

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

PDO实现删除操作(参考更新操作独立完成)


实例

<?php
/**
 * PDO
 */
$pdo = new PDO('mysql:dbname=php', 'root', 'root');

$sql = "delete from staff where staff_id=:staff_id";

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

$stmt->execute(['staff_id'=>34]);
$pdo = null;

运行实例 »

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


Correction status:Uncorrected

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
  • 1
    2018-03-16 00:39:40