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(); }
运行实例 »
点击 "运行实例" 按钮查看在线实例