Programming MySQLi prepared statements I'm programming code with MySQLi prepared statements. I'm a beginner and just want to ask to experts if my code is correct.

function getPaymentMethodName( $id ) {
    global $mysqli;
    if ( $stmt = $mysqli-&gt;prepare( 'SELECT name FROM `payment_methods` WHERE id = ?' ) ) {
        if ( $stmt-&gt;bind_param( 'i', $id ) ) {
            if ( $stmt-&gt;execute() ) {
                $stmt-&gt;bind_result( $name);
                if ( $stmt-&gt;fetch() ) {
                    $stmt-&gt;close();
                    return $name;
                }
            }
        }
    }
    $stmt-&gt;close();
    return false;
}
This is really pretty good. 
I would do a few things slightly differently.


Don't use globals. Pass the $mysqli parameter into your function. Globals cause nightmares for maintainers.
Do an explicit error check after each mysqli method invocation, and get rid of the cascading if statements. Check for a false return, then issue a message.  In your logic you might also emit the error message and then return false. But you need to announce the error somehow or other or you'll have trouble debugging things.


For example: (Note, using die is a little sloppy, but not as bad as failing to report errors.) 

 $stmt = $mysqli-&gt;prepare( 'SELECT name FROM `payment_methods` WHERE id = ?' );
 if ($stmt === false) {
         die "prepare failed:" . mysqli_connect_error();

 }