实例
<?php // error_reporting(E_ALL^E_NOTICE); // // 1.参数 $host = '127.0.0.1'; $dbname = 'MyTest'; $user = 'root'; $pw = '13650158099'; //2.创建连接对象 $mysqli_obj = new mysqli($host,$user,$pw,$dbname); //3.判断是否边接成功 if ($mysqli_obj->connect_errno) { exit('连接MySQL失败!\n'.'错误代码'.$mysqli_obj->connect_errno.':'.$mysqli_obj->connect_error); } else { echo '<h3>连接MySQL成功!</h3>'; } //4.insert sql $sql = "INSERT INTO bs_supplier (keyid,name) VALUES (?,?)"; $stmt = $mysqli_obj->stmt_init(); if ($stmt->prepare($sql)) { $stmt->bind_param('ss',$keyid,$name); $keyid = 'obj1'; $name = 'test_mysqli_obj'; $stmt->execute(); $i = $stmt->affected_rows; if ($i > 0) { echo '成功新增了'.$i.'笔记录!'; } else { echo '没有新增记录!'; } } else { exit('错误代码'.$stmt->errno.':'.$stmt->error); } echo '<br>'; //UPDATE 操作 // 1.sql准备 $sql = "UPDATE bs_supplier SET name = CONCAT(name,?) WHERE KeyId = ?"; // 2.创建stmt对象 $stmt = $mysqli_obj->stmt_init(); // 3.预处理 if ($stmt->prepare($sql)) { // 4.绑定参数 $stmt->bind_param('ss',$para1,$para2); $para1 = '_objUpdate'; $para2 = 'obj1'; // 5.执行sql $stmt->execute(); $i = $stmt->affected_rows; if ($i > 0) { echo '成功更新了'.$i.'笔记录!'; } else { echo '没有记录被更新!'; } } else { exit('错误代码'.$stmt->errno.':'.$stmt->error); } echo '<br>'; //DELETE sql //1.sql准备 $sql = "DELETE FROM bs_supplier WHERE KeyId = ?"; //2.创建stmt对象 $stmt = $mysqli_obj->stmt_init(); //3.预处理 if ($stmt->prepare($sql)) { //4.绑定参数 $stmt->bind_param('s',$para1); $para1 = 'obj1'; //5.执行sql $stmt->execute(); $i = $stmt->affected_rows; if ($i > 0) { echo '成功删除了'.$i.'笔记录!'; } else { echo '没有记录被删除!'; } } else { exit('错误代码'.$stmt->errno.':'.$stmt->error); } $mysqli_obj->close(); ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php // 连接mssql // 1.数据库参数 $dbType = 'sqlsrv'; $host = '127.0.0.1'; $dbname = 'WsErp'; $user = 'sa'; $pw ='password'; $dsn = "{$dbType}:server={$host};database={$dbname}"; try{ //2.连接数据库 $pdo = new PDO($dsn,$user,$pw); echo "<h3>连接MsSQL成功!</h3>"; //3.Sql语句 INSERT $sql = "INSERT INTO bs_supplier (keyid,name) VALUES (:keyid,:name)"; //4.创建stmt $stmt = $pdo->prepare($sql); //5.绑定参数执行 $param = ['keyid'=>'test1','name'=>'test_name']; $stmt->execute($param); //6.取数据集等相关信息 $NewId = $pdo->lastInsertId(); $num = $stmt->rowCount(); if ($num > 0) { echo '成功插入了'.$num.'笔数据:'.'ID是' .$NewId; } else { echo '<h3>插入失败</h3>'; print_r($stmt->errorInfo()); exit; } echo '<br>'; //--UPDATE----- //3.Sql语句 UPDATE $sql = "UPDATE bs_supplier SET name = name +:name WHERE KeyId = :keyid"; //4.创建stmt $stmt = $pdo->prepare($sql); //5.绑定参数执行 $param = ['keyid'=>'test1','name'=>'_pdoUpdate']; $stmt->execute($param); //6.取数据集等相关信息 $num = $stmt->rowCount(); if ($num > 0) { echo '成功更新了'.$num.'笔数据!'; } else { echo '<h3>更新失败</h3>'; print_r($stmt->errorInfo()); exit; } echo '<br>'; //-----DELETE----- //3.Sql语句 DELETE $sql = "DELETE FROM bs_supplier WHERE KeyId = :keyid"; //4.创建stmt $stmt = $pdo->prepare($sql); //5.绑定参数执行 $param = ['keyid'=>'test1']; $stmt->execute($param); //6.取数据集等相关信息 $num = $stmt->rowCount(); if ($num > 0) { echo '成功删除了'.$num.'笔数据!'; } else { echo '<h3>删除失败</h3>'; print_r($stmt->errorInfo()); exit; } echo '<br>'; }catch (PDOException $e){ die('操作失败。'.$e->getMessage()); } unset($pdo); $pdo = null; ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例