mysqli_num_rows失败,带有准备好的语句,过程样式

问题描述 投票:1回答:1

据我所知,使用准备好的语句的好处是可以防止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个回答
1
投票

我认为您正在与mysqli_stmt_num_rows-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 );
?>
© www.soinside.com 2019 - 2024. All rights reserved.