Php Object of class mysqli could not be converted to string

Php Object of class mysqli could not be converted to string,php,mysqli,Php,Mysqli,I had my old website so I've decided to change it from mysql_ to mysqli so I've managed to complete 40% and now i am stuck with this problem.Help Me! I am getting error on 'implode()' function function user_data($user_id,$conn){ $data = array(); $user_id = (int)$user_id; $func_num_args = func_num_args(); $func_get_args = func_get_args(); if ($func_num_args > 1){ unset($func_get_args[0]); $fields = '`'.implode('`,`',$func_get_args).'`'; $query = "SELECT ".$fields." FROM users

I had my old website so I've decided to change it from mysql_ to mysqli so I've managed to complete 40% and now i am stuck with this problem.Help Me!

I am getting error on 'implode()' function

function user_data($user_id,$conn){
$data = array();
$user_id = (int)$user_id;

$func_num_args = func_num_args();
$func_get_args = func_get_args();

if ($func_num_args > 1){

    unset($func_get_args[0]);
    $fields = '`'.implode('`,`',$func_get_args).'`';
    $query = "SELECT ".$fields." FROM users WHERE user_id = ".$user_id."";
    $result = mysqli_query($conn,$query) or die(mysqli_error($conn));
    while ($row = $result->fetch_assoc()) {
    $data = $row['user_id'];
    }

    return $data;
    }
}

In order to get below code to work properly

if (logged_in() === true){
$session_user_id = $_SESSION['user_id'];
$user_data = user_data($session_user_id,'user_id','username',
            'password','first_name','last_name','email','type',$conn);
}

Any alternate way to perform same task will


#1

You have not correct definition of user_data function,

In it's signature you have only two arguments:

function user_data($user_id, $conn)

So, these arguments are $user_id and $conn.

But when you call your user_data you pass more than 2 arguments:

user_data($session_user_id,'user_id','username', 'password','first_name','last_name','email','type',$conn);

See, you have 8 arguments here. And $conn is not the second one, it's eighth!

And when you do

$fields = '`'.implode('`,`',$func_get_args).'`';

last argument which holds your mysqli-connection is being added to $fields.

So, you have to rewrite your function, for example this way:

function user_data($user_id, $conn, $fields) {
    $data = array();
    $user_id = (int)$user_id;

    $fields = '`'.implode('`,`', $fields).'`';
    $query = "SELECT ".$fields." FROM users WHERE user_id = ".$user_id."";
    $result = mysqli_query($conn, $query) or die(mysqli_error($conn));
    while ($row = $result->fetch_assoc()) {
        $data = $row['user_id'];
    }

    return $data;
}

And call it for example:

$user_data = user_data(
    $session_user_id,   // $user_id
    $conn,              // $conn
    array('user_id','username','password','first_name','last_name','email','type')    // fields as ARRAY
);

#2

$func_get_args has $user_id, other string values and at last the mysqli connection object. You must unset last element of function parameters. Correct user_data function is that:

function user_data($user_id,$conn){
$data = array();
$user_id = (int)$user_id;

$func_num_args = func_num_args();
$func_get_args = func_get_args();

if ($func_num_args > 1){

    unset($func_get_args[0]);
    unset($func_get_args[ $func_num_args - 1]); // you must delete last element becouse this is mysqli object
    $fields = '`'.implode('`,`',$func_get_args).'`';
    $query = "SELECT ".$fields." FROM users WHERE user_id = ".$user_id."";
    $result = mysqli_query($conn,$query) or die(mysqli_error($conn));
    while ($row = $result->fetch_assoc()) {
    $data = $row['user_id'];
    }

    return $data;
    }
}

#3

what error you get ??

#4

Catchable fatal error: Object of class mysqli could not be converted to string

#5

please see error o which line it throws and point that line here ... Not the function name,

#6

8th line $fields = ''.implode(',',$func_get_args).'';

#7

this code looks unsafe to use in a live environment

#8

Thanks bro , you made my day