PHP函数无法连接到DB [关闭]

I have this piece of code:

<?php
require('includes/db.php');

function connectHouseObjects() {

        /* Register a prepared statement */
        if ($stmt = $mysqli->prepare('SELECT * FROM house_room1 WHERE user_id = ?')) {

            /* Bind parametres */
            $stmt->bind_param('i', $user_id);

                /* Insert the parameter values */
                $user_id = 1;

                /* Execute the query */
                $stmt->execute();

                /* Close statement */
                $stmt->close();

            } else {
                /* Something went wrong */
                echo 'Something went terrible wrong'     . $mysqli->error;
            }

        }
?>

And I use this in a seperate document to call it:

require('main_database.php');
connectHouseObjects();

Soo I get an error on this line: if ($stmt = $mysqli->prepare('SELECT * FROM house_room1 WHERE user_id = ?')) {

saying: Fatal error: Call to a member function prepare() on a non-object in .../x/main_database.php on line 7.

Any ideas? Anything is appreciated. Thanks in advance.

Basic PHP syntax:

function connectHouseObjects() {

        /* Register a prepared statement */
        if ($stmt = $mysqli->etc....
                    ^^^^^^---undefined in your function

You need to do

   global $mysqli;

within your function before you actually use that connection handle.