我正在学习使用
MySQL的OOP
PHP.我试图运行一个应该打印返回的行数的查询.我的PHP代码如下:
@H_502_1@$con= MysqLi_connect('localhost','root','','dealinte_cms') or die(MysqLi_error());
$email="joy1@gmail.com";
if($r=$con->prepare("SELECT * FROM user WHERE email=? ")){
$r->bind_param("s",$email);
if(!$r->execute()){
echo MysqLi_errno($con);
}
else{
$rowCount=$r->num_rows;
echo $rowCount;
}
}
在我的数据库中,电子邮件是4行,所以它应该打印4,但它显示0
我的代码有什么问题?
你必须
store result
@H_502_1@$r->store_result();
在检查行数之前,也是如此.请参阅手册页上的此注释
Please be advised,for people who sometimes miss to read this important Manual entry for this function:
If you do not use MysqLi_stmt_store_result( ),and immediatley call this function after executing a prepared statement,this function will usually return 0 as it has no way to know how many rows are in the result set as the result set is not saved in memory yet.
小费
由于您正在使用MysqL学习OOP PHP,因此请务必注意,您为MysqL创建的连接采用过程式语法.虽然在PHP中你可能会侥幸逃脱,但在许多其他语言中你不会.为什么不立即将其转换为OOP语法?
@H_502_1@$con= new MysqLi('localhost','my_user','my_password','my_db');