简体   繁体   English

返回数组和num行mysqli准备

[英]Returning array and num rows mysqli prepared

I'm a bit new to the mysqli prepared statement and I would like to use fetch_array to return the results AND also return num_rows as an array value. 我对mysqli prepare语句有点陌生,我想使用fetch_array返回结果,并且还返回num_rows作为数组值。

I have something like this 我有这样的东西

function getCategories($dbh, $catId) 
{
    $data = array();
    $s = "SELECT id, title FROM categories WHERE parent_id = ?";
    if ($stmt = mysqli_prepare($dbh, $s)) {
        mysqli_stmt_bind_param($stmt, "i", $catId);
        mysqli_stmt_execute($stmt);
        if (mysqli_stmt_errno($stmt)) {
            exit(mysqli_stmt_error($stmt));
        }
        mysqli_stmt_store_result($stmt);
        $count = mysqli_stmt_num_rows($stmt)) {
        if ($count) {
            $data['count'] = $count;
            $result = mysqli_stmt_get_result($stmt);
            while ($r = mysqli_fetch_assoc($result)) {
                $data[] = $r;
            }
        }

        return $data;
    } else {
        exit(mysqli_error($dbh));
    }
}

It seems that I cannot use mysqli_stmt_store_result and mysqli_stmt_get_result(). 看来我无法使用mysqli_stmt_store_result和mysqli_stmt_get_result()。

The store_result function seems to give a boolean and then I get this error: "mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given" store_result函数似乎提供了一个布尔值,然后出现此错误: “ mysqli_fetch_assoc()期望参数1为mysqli_result,给定布尔值”

Hope this makes sense. 希望这是有道理的。 Any help would be really appreciated. 任何帮助将非常感激。

Updated: 更新:

function getCategories($dbh, $catId) 
{
    $data = array();
    $s = "SELECT id, title FROM categories WHERE parent_id = ?";
    if ($stmt = mysqli_prepare($dbh, $s)) {
        mysqli_stmt_bind_param($stmt, "i", $catId);
        mysqli_stmt_execute($stmt);
        if (mysqli_stmt_errno($stmt)) {
            exit(mysqli_stmt_error($stmt));
        }

        $result = mysqli_stmt_get_result($stmt);
        $data['count'] = $result->num_rows;
        while ($r = mysqli_fetch_assoc($result)) {
            $data[] = $r;
        }

        return $data;
    } else {
        exit(mysqli_error($dbh));
    }
}

According to the PHP docs, mysqli_stmt_get_result returns FALSE on error: 根据PHP文档, mysqli_stmt_get_result在错误时返回FALSE

Returns a resultset for successful SELECT queries, or FALSE for other DML queries or on failure. 返回成功的SELECT查询其他DML查询或失败的一个结果,或FALSE。 The mysqli_errno() function can be used to distinguish between the two types of failure. mysqli_errno()函数可用于区分两种类型的故障。

You're then passing that into mysqli_fetch_assoc which complains because you're giving it a bool instead of the resultset it expects. 然后,您将其传递到mysqli_fetch_assoc ,这是抱怨的,因为您给它的是布尔值而不是预期的结果集。

Do a little more erroring checking at that point and you'll be fine. 到那时再进行一些错误检查,就可以了。 There's probably something wrong with your SQL query. 您的SQL查询可能出问题了。 Call mysqli_errorno to determine if there's an error, as the docs state above. 调用mysqli_errorno以确定是否存在错误,如上面的文档所述。

EDIT: 编辑:

Use the mysqli_error function to get a description of the mysql error. 使用mysqli_error函数获取mysql错误的描述。 It would be best to use this everywhere you're checking for failure as having an error message will make debugging much easier than simply failing silently. 最好在要检查故障的地方都使用此功能,因为有一条错误消息比不进行静默故障要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM