首页 > 代码库 > MYSQLI DEMO
MYSQLI DEMO
1、Select
// DEMO mysqli连接方式参考$db = new mysqli("localhost:3306", "root", "", "dab"); // 简单查询//$result = $db -> query("select * from DAB_NTOS_ADMIN");//while($row = $result -> fetch_row()) {// echo var_dump($row);//}$start = 0;$limit = 1;// 防sql注入$stmt = $db -> prepare("select * from DAB_NTOS_ADMIN limit ?,?");$stmt -> bind_param("ii", $start, $limit);$stmt -> execute(); $admin = new DAB_NTOS_ADMIN();$stmt -> bind_result($admin -> id,$admin -> name,$admin -> pwd,$admin -> telephone,$admin -> email,$admin -> autor_date,$admin -> descr,$admin -> realName);while ($stmt->fetch()) { echo var_dump($admin);} $db -> close();
2、Update
$db = new mysqli("localhost:3306", "root", "", "dab"); // 防sql注入$stmt = $db -> prepare("update DAB_NTOS_ADMIN set name = ? where id = ?");$id = 1;$name = ‘dab‘;$stmt -> bind_param("si", $name, $id);$result = $stmt -> execute();echo $result; $db -> close();
3、Insert
$db = new mysqli("localhost:3306", "root", "", "dab"); // 防sql注入$stmt = $db -> prepare("insert into DAB_NTOS_ADMIN (name) values (?)");$name = ‘dab‘;$stmt -> bind_param("s", $name);$result = $stmt -> execute();echo $result; $db -> close();
4、Delete
// DEMO mysqli连接方式参考$db = new mysqli("localhost:3306", "root", "", "dab"); // 防sql注入$stmt = $db -> prepare("delete from DAB_NTOS_ADMIN where id = ?");$id = 2;$stmt -> bind_param("i", $id);$result = $stmt -> execute();echo $result; $db -> close();
事务管理参考
try { $db = new mysqli("localhost:3306", "root", "", "dab"); $db -> autocommit(0); // 防sql注入 $stmt = $db -> prepare("insert into DAB_NTOS_ADMIN (name) values (?)"); $name = ‘dab‘; $stmt -> bind_param("s", $name); $result = $stmt -> execute(); $db -> commit(); echo $result; } catch (Exception $e) { $db -> rollback();} finally { // finally可能会报编译时错误,目前好像支持php5.5,而淡蓝的ide环境用的是5.3,服务器上的是5.5,所以编码的时候不行,发布之后就可以用了 $db -> close();}
MYSQLI DEMO
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。