在非对象上调用成员函数prepare()[重复]

This question already has an answer here:

<?php

$dob = $_SESSION['dob'];
$month = $_SESSION['month'];
$s = $_POST['present'];
$p = "1";

if ($stmt = $mysqli->prepare("UPDATE atten SET total = ? WHERE name = ?")) 
{
    // Bind the variables to the parameter as strings. 
    foreach ($s as $name) 
    {
        $stmt->bind_param("ss", $p, $name);

        // Execute the statement.
        $stmt->execute(); 
    }

    // Close the prepared statement.
    $stmt->close();
}

This is the error message I'm getting when I try to execute the above code:

Call to a member function prepare() on a non-object.

Any ideas why?

</div>

You have to instantiate the mysqli object before you can use it

<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'database');
$mysqli->prepare('SQL STATEMENT');

First you need to create a database connection and then only you can make queries from the database. like

$mysqli = new mysqli('localhost', 'username', 'password', 'database');

Have a look at Example #1 here on how to create a mysqli object:

http://www.php.net/manual/en/mysqli.construct.php