数据库面向对象连接
实例
<?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;
运行实例 »
点击 "运行实例" 按钮查看在线实例