Zuckerberg Zuckerberg - 1 month ago 6
MySQL Question

Prepared statements not working in a function with MYSQLI EXTENTION

I have now spent over 3 hours trying to make this code work properly with mysqli extention in php. I already know that there is a user with userid 4 but when i try to use mysql prepared statement with [MYSQLI EXT], the code fails,and if it passes the test, i only see a blank page. Somebody please help me.

The code snipe is below in php.

<?php

//testing...
require_once('connections/connections.php');
ini_set('display_errors',true);
$received_id=4;//from the client...

function synchronize(){
global $con, $received_id;

$check_db=$con->prepare("SELECT FIRSTNAME,LASTNAME,USERNAME FROM SUPERDATATABLE WHERE USERID=?");
$check_db->bind_param('i',$received_id);
$check_db->execute();

if($check_db->num_rows>0){
$check_db->bind_result($first_name,$last_name,$user_name);
$check_db->fetch();
echo $first_name.'+'.$last_name.'+'.$user_name;
}else{
echo 'Sorry, but this did not work';
}

}synchronize();

Answer

you are missing in store_results() function. please try the code below and see if it works for you but i have tested it.

<?php
//testing...
require_once('connections/connections.php');
ini_set('display_errors',true);

$received_id=4;//from the client...


function synchronize(){
    global $con, $received_id;

    $check_db=$con->prepare("SELECT FIRSTNAME,LASTNAME,USERNAME FROM SUPERDATATABLE WHERE USERID=?");
    $check_db->bind_param('i',$received_id);
    $check_db->execute();


    #typically required....
    #you are missing this statement which is required to check the length of selected rows
    $check_db->store_result();


    if($check_db->num_rows>0){
        $check_db->bind_result($first_name,$last_name,$user_name);
        $check_db->fetch();
        echo $first_name.'+'.$last_name.'+'.$user_name;
    }else{
        echo 'Sorry, but this did not work';
    }

}synchronize();
Comments