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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制