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();

}

 

你可能感兴趣的:(mysql)