mysqli_num_rows失败,并带有预处理语句,过程样式 [英] mysqli_num_rows fails with prepared statement , procedural style

查看:98
本文介绍了mysqli_num_rows失败,并带有预处理语句,过程样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,使用准备好的语句的好处是可以防止SQL注入.

I as far as I understood, the advantage of using prepared statements is to prevent SQL injections.

我设法使用SELECT和INSERT用准备好的语句构建查询.

I managed to build queries with prepared statements using SELECT and INSERT.

但是要达到select count()的等效值,我会把头撞在墙上.
PHP手册提供:

But to achieve the equivalent of select count() , and I'm banging my head against the wall.
The PHP manual gives:

if ($result = mysqli_query($link, "SELECT Code, Name FROM Country ORDER BY Name")) {

    /* determine number of rows result set */
    $row_cnt = mysqli_num_rows($result);

    printf("Result set has %d rows.\n", $row_cnt);

    /* close result set */
    mysqli_free_result($result);
}

我也尝试使用准备好的语句来执行此操作.但是也许我不应该?

I trying to do this with prepared statement too. But maybe I shoudn't ?

这是我正在尝试的:

$boy = 'yes';
$age   = 1;

$result = mysqli_prepare ($bdd, 'SELECT boy , age FROM photo WHERE  boy = ? AND age= ?' );

mysqli_stmt_bind_param( $result, "si", $boy , $age );
mysqli_stmt_execute( $result );

$row_cnt = mysqli_num_rows( $result );
printf( "Le jeu de résultats a %d lignes.\n", $row_cnt );

但是无论我尝试什么,我总是会遇到相同类型的错误

But I always get the same type of error whatever I'am trying

警告:mysqli_num_rows()期望参数1为mysqli_result,第36行的C:\ wamp \ www \ page.com \ pic.php中给出的对象

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given in C:\wamp\www\page.com\pic.php on line 36

推荐答案

我认为您正在与mysqli_stmt_store_result结合使用mysqli_stmt_num_rows-

I think you're looking for mysqli_stmt_num_rows in combination with mysqli_stmt_store_result - http://www.php.net/manual/en/mysqli-result.num-rows.php

<?php
$boy = 'yes';
$age   = 1;

$result = mysqli_prepare ($bdd, 'SELECT boy , age FROM photo WHERE  boy = ? AND age= ?' );

mysqli_stmt_bind_param( $result, "si", $boy , $age );
mysqli_stmt_execute( $result );

// You may need this too...
mysqli_stmt_store_result( $result );

$row_cnt = mysqli_stmt_num_rows( $result );
printf( "Le jeu de résultats a %d lignes.\n", $row_cnt );
?>

这篇关于mysqli_num_rows失败,并带有预处理语句,过程样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆