您的位置:首页 > 其它

使用预处理语句实现数据查询的方法

2015-06-01 20:18 686 查看
查询数据库里面有多少条数据
$m=new mysqli('localhost','root','','db');
$m->set_charset('utf8');
$stmt=$m->prepare('select count(*) from stu');
$stmt->execute();
$stmt->bind_result($c);
$stmt->fetch();
echo $c;
$stmt->free_result();
$stmt->close();
$m->close();
使用预处理语句实现数据的查询方法一:
$m=new mysqli('localhost','root','','db');

$m->set_charset('utf8');

$stmt=$m->prepare('select * from stu where 1=1');

$stmt->execute();

$stmt->bind_result($id,$name,$sgender,$sscore);

while($stmt->fetch()){

echo "$id,$name,$sgender,$sscore".'<br>';

}

$stmt->free_result();

$stmt->close();

$m->close();

[/code]使用预处理语句实现数据的查询方法二:
$m=new mysqli('localhost','root','','db');

$m->set_charset('utf8');

$stmt=$m->prepare('select * from stu where 1=1');

$stmt->execute();

$result=$stmt->get_result();

$rows=$result->fetch_all(2);

foreach($rows as $v){

print_r($v).'<br>';

}


$stmt->free_result();

$stmt->close();

$m->close();

[/code]使用预处理语句实现数据的查询方法三:
$m=new mysqli('localhost','root','','db');
$m->set_charset('utf8');
$stmt=$m->prepare('select * from stu where sid=?');
$n=10;
$stmt->bind_param('i',$n);
$stmt->execute();
$stmt->bind_result($id,$name,$sgender,$sscore);
$stmt->fetch();
echo $id,$name,$sgender,$sscore;
$stmt->free_result();
$stmt->close();
$m->close();


来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: