Alan
Alan

Reputation: 279

PHP MYSQLI Prepared Statement returns 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

Upvotes: 1

Views: 5293

Answers (2)

shacharsol
shacharsol

Reputation: 2342

I Had the same problem the reason for that is closing the connection when using the statement prepare. make sure, in your flow there is no such closing of the connection

Upvotes: 1

JWMarchant
JWMarchant

Reputation: 356

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

Upvotes: 5

Related Questions