码迷,mamicode.com
首页 > 数据库 > 详细

MYSQLI DEMO

时间:2014-08-25 11:27:24      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   ar   art   div   

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

标签:des   style   blog   color   os   io   ar   art   div   

原文地址:http://www.cnblogs.com/gabin/p/3934419.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!