【问题标题】:mysqli_num_rows fails with prepared statement , procedural stylemysqli_num_rows 因准备好的语句而失败,程序风格
【发布时间】:2013-02-21 00:09:46
【问题描述】:

据我了解,使用预处理语句的好处是可以防止 SQL 注入。

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

但要达到相当于 select count() 的效果,我的头撞墙了。
PHP手册给出:

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

我也尝试使用准备好的语句来做到这一点。但也许我不应该?

这就是我正在尝试的:

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

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

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

【问题讨论】:

    标签: php mysqli prepared-statement


    【解决方案1】:

    我认为您正在寻找 mysqli_stmt_num_rowsmysqli_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_ stmt store_result( $result ); mysqli stmt _num_rows($result);非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2014-01-29
    • 2014-03-16
    • 2017-03-08
    • 2018-02-21
    • 2019-06-15
    相关资源
    最近更新 更多