Blogger Information
Blog 42
fans 4
comment 0
visits 30554
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
4.25 php mysqli面向对象及PDO入门 --27Day
小丑的博客
Original
612 people have browsed it

1.mysqli 面向对象
创建数据库链接

实例

<?php


$URL = '127.0.0.1';
$dbName = 'php';
$userName = 'root';
$passWord = 'root';
$char = 'utf8';

$mysql = new mysqli($URL,$userName,$passWord,$dbName);

if($mysql->connect_errno){
    echo '连接失败:'.$mysql->error;
}

$mysql->set_charset($char);

运行实例 »

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

2.更新操作

实例

<?php


require 'mysqli-obj.php';

$stmt = $mysql->stmt_init();

$sql = "update staff set iphone=? where name=?";

if($stmt->prepare($sql)){

    $stmt->bind_param('ss',$iphone,$name);
    $iphone='777';
    $name='女娲';

    $stmt->execute();

    if($stmt->affected_rows>0){

        echo '更新成功:'.$stmt->affected_rows;

    }else{

        echo '更新失败';

    }


}else{

    echo $stmt->error;

}

运行实例 »

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


2.PDO查询操作

实例

<?php

require 'pdo_connect.php';


$sql = 'select name,age,address,iphone from staff where staff_id>:id;';

if($stmt = $pdo->prepare($sql)){

    $stmt->bindParam('i',$id);

    $data = ['id'=>6];

    if($stmt->execute($data)){

        echo '<table border=1><tr><td>姓名</td><td>年龄</td><td>国籍</td><td>联系电话</td></tr>';

        while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
            echo '<tr><td>'.$res['name'].'</td><td>'.$res['age'].'</td><td>'.$res['address'].'</td><td>'.$res['iphone'].'</td></tr>';
        }

        echo '</table>';

    }else{

        echo '查询失败,无数据';
        die();
    }

}else{

    echo $pdo->errorInfo();

}

运行实例 »

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


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