实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/26 0026 * Time: 上午 11:40 */ header("content-type:text/html;charset=utf-8"); $dsn = 'mysql:host=localhost;dbname=testguest'; $dbname = 'root'; $dbpass = 'root'; try{ $pdo = new PDO($dsn,$dbname,$dbpass); }catch (PDOException $e){ die("错误信息".$e->getMessage()); }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/26 0026 * Time: 上午 11:44 */ header("content-type:text/html;charset=utf-8"); /** * select * @param $pdo * @param $sql * @param $id */ function pdoSelect($pdo,$sql,$id){ if ($sth = $pdo->prepare($sql)) { $id = 0; $sth->bindParam(':id', $id, PDO::PARAM_INT); $sth->execute(); echo "<pre>"; if ($result = $sth->fetchAll()){ print_r($result); } }else{ die($pdo->errorInfo()); } $pdo = null; } /** * insert * @param $pdo * @param $sql * @param $username * @param $password * @param $question */ function pdoInsert($pdo,$sql,$username,$password,$question){ if ($sth = $pdo->prepare($sql)) { $sth->bindParam(':username', $username, PDO::PARAM_STR); $sth->bindParam(':password', $password, PDO::PARAM_STR); $sth->bindParam(':question', $question, PDO::PARAM_STR); $sth->execute(); if ($sth->rowCount()>0){ echo "插入成功"; } }else{ die($pdo->errorInfo()); } $pdo = null; } /** * update * @param $pdo * @param $sql * @param $username * @param $password * @param $question * @param $id */ function pdoUpdate($pdo,$sql,$username,$password,$question,$id){ if ($sth = $pdo->prepare($sql)) { $sth->bindParam(':username', $username, PDO::PARAM_STR); $sth->bindParam(':password', $password, PDO::PARAM_STR); $sth->bindParam(':question', $question, PDO::PARAM_STR); $sth->bindParam(':id', $id, PDO::PARAM_INT); $sth->execute(); if ($sth->rowCount()>0){ echo "更新成功"; } }else{ die($pdo->errorInfo()); } $pdo = null; } /** * delete * @param $pdo * @param $sql * @param $id */ function pdoDelete($pdo,$sql,$id){ if ($sth = $pdo->prepare($sql)) { $sth->bindParam(':id', $id, PDO::PARAM_INT); $sth->execute(); if ($sth->rowCount()>0){ echo "删除成功"; } }else{ die($pdo->errorInfo()); } $pdo = null; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/26 0026 * Time: 下午 1:56 */ require "common/connect.php"; require "common/function.php"; $sql = 'DELETE FROM tg_user WHERE dg_id>=:id'; pdoDelete($pdo,$sql,$id=3);
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/26 0026 * Time: 上午 11:45 */ require "common/connect.php"; require "common/function.php"; $sql = 'INSERT tg_user SET dg_username=:username,dg_password=sha1(:password),dg_question=:question'; pdoInsert($pdo,$sql,$username="admin_" . time(),$password=sha1(123456),$question="我是谁?");
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/26 0026 * Time: 下午 2:01 */ require "common/connect.php"; require "common/function.php"; $sql = 'SELECT * FROM tg_user WHERE dg_id >=:id'; pdoSelect($pdo,$sql,$id=0);
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/26 0026 * Time: 下午 1:56 */ require "common/connect.php"; require "common/function.php"; $sql = 'UPDATE tg_user SET dg_username=:username,dg_password=sha1(:password),dg_question=:question WHERE dg_id=:id'; pdoUpdate($pdo,$sql,$username="admin000_" . time(),$paswword="123456",$question = "这是啥aaa",$id=3);
运行实例 »
点击 "运行实例" 按钮查看在线实例