Msqli php query update doesn't work, returns no errors

I wrote the following code:

 public static function updateUser($userid, $username){
 $query = 'UPDATE users SET username=? WHERE id=?';
    $statement =  $GLOBALS["DB"]->prepare($query);
    $statement->bind_param('is', $userid, $username);
    $statement->execute();
    $statement->store_result();

    if ($statement->affected_rows == 1){
        return  1;
    }
    else{
        return $statement;
    }}

      

The table names are correct, the data I pass inside the method is fine too, but it fails and returns no error. When I try to return these instructions to the console, it gives me this:

mysqli_stmt Object ([affected_rows] => 0 [insert_id] => 0 [num_rows] => 0 [param_count] => 2 [field_count] => 0 [errno] => 0 [error] => [error_list] => Array () [sqlstate] => 00000 [id] => 3) {"Status": "Success"}

I checked the error list, it is empty. Do I need to install something to keep it updated? Does anyone have an idea what this might be? The database is created as follows:

<?php
    require_once ("simplecms-config.php");
    require_once ("./Functions/database.php");

    // Create database connection
    $databaseConnection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if ($databaseConnection->connect_error)
    {
        die("Database selection failed: " . $databaseConnection->connect_error);
    }
    // Create tables if needed.
    prep_DB_content();
    $GLOBALS['DB'] = $databaseConnection ;
?>

      

+3


source to share


2 answers


The parameter list bind_param

does not match.

Change

$statement->bind_param('is', $userid, $username);

      



For

$statement->bind_param('si', $username, $userid);

      

+2


source


The list of parameters in bind_param

must correspond to those contained in the request itself.

So how does the query have the username as the first one? and userid as the second ?, this is what the bind_param call should use:



$statement->bind_param('si', $username, $userid);

+3


source







All Articles