dongre6404 2012-03-14 14:18
浏览 55
已采纳

PHP MYSQLI Prepared语句返回NULL

I have a query that works in phpmyadmin however does not work in my code! I have tried various variable dumps to see if I have been loosing data before the query is executed, all seems ok, the same variables contents I have used in the successful query in phpmyadmin

To test I replaced:

$account_id = $account->getAccountId(); //output below
string(2) "59" string(4) "main" NULL NULL array(2) { ["id"]=> NULL["name"]=>NULL}

With

$account_id = 59; //output below
int(59) string(4) "main" NULL NULL array(2) { ["id"]=> NULL ["name"]=> NULL }

below is the code extract and I am using mysqli:

        $account = $Add_Profile_Image->getUserAccount();
        $account_id = $account->getAccountId();
        $status = $account->getType();
        var_dump($account_id);
        var_dump($status);
        $conn = $this->create_connection('read');
        $stmt = $conn->prepare('SELECT add_profile_images.image_id, image_name FROM add_profile_images, users_profile_images WHERE users_profile_images.account_id=? AND users_profile_images.status=?');
        $stmt->bind_param('is',$account_id,$status);
        $stmt->bind_result($id,$imageName);
        $stmt->execute();
        var_dump($id);
        var_dump($imageName);
        $result['id'] = $id;
        $result['name'] = $imageName;

I have replaced

image_name //in the query

To

add_profile_images.image_name //in the query

but the result is still NULL?

So I have tried the following examples in this post: PHP Prepared Statement Returns -1 When I dump the mysqli object it does return -1 however when I implement the below no errors are shown!

if($conn->connect_error) {
            printf('connect error (%d) %s', $conn->connect_errno, htmlspecialchars($conn->connect_error));
                        die;
        }
        $stmt = $conn->prepare('SELECT add_profile_images.image_id, add_profile_images.image_name FROM add_profile_images, users_profile_images WHERE users_profile_images.account_id=? AND users_profile_images.status=?');
        if ( false===$stmt ) {
            printf('prepare failed: %s', htmlspecialchars($conn->error));
            die;
        }

        $rc = $stmt->bind_param('is',$account_id,$status);
        if ( false===$rc ) {
            printf('bind_param failed: %s', htmlspecialchars($stmt->error));
        die;
        }

        $rc= $stmt->execute();
        if ( false===$rc ) {
                printf('execute failed: %s', htmlspecialchars($stmt->error));
                die;
        }
        $rc = $stmt->bind_result($id,$imageName);
        if ( false===$rc ) {
                printf('bind_result failed: %s', htmlspecialchars($stmt->error));
                die;
        }

Where am I going wrong?

Hope someone can help!

Thanks

  • 写回答

2条回答 默认 最新

  • dquoj04882 2012-03-14 14:42
    关注

    You need to state $stmt->bind_result() AFTER $stmt->execute() (see: http://php.net/manual/en/mysqli-stmt.bind-result.php)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程