ParisNakitaKejser
ParisNakitaKejser

Reputation: 14979

mysqli using prepare

I need some help, I have created this function and it works fine, but now I need to use "prepare" in MySQLi. Can I get some help?

I have tried http://dk.php.net/manual/en/mysqli.prepare.php but I don't really understand how to prepare work with MySQLi.

My db obj is ( $this->db ) in my class.

I need a sample of my own code here, and if I want to insert/delete and update data. :)

Thanks a lot to all.

public function query( $sql , $multi = false )
    {
        // if query is ok
        if ( $q = $this->db->query( $sql ) ) 
        {
            if ( $multi == true )
            {
                // multi fetch make in array/object
                while( $d = $q->fetch_object() )
                {
                    $obj[] = $d;
                }
            }
            else
            {
                // single fetch
                $obj = $q->fetch_object();
            }
            return $obj;
        }
        // if query fail print error
        else
        {
            echo $this->db->error;
            return false;
        }
    }

Upvotes: 1

Views: 624

Answers (1)

Tyler Carter
Tyler Carter

Reputation: 61597

$mysqli = new mysql("host", "user", "password", "location");
$statement = $mysqli->prepare("SELECT * FROM TABLE WHERE ID=?");
$statement->bind_param("s","5");
$statement->execute();
$result = $statement->result_metadata;
$object = $result->fetch_object();

Basically, you put where you want values to go with a ?, and then bind them in. The S in the beginning I belive means string.

To find out more about preparing stuff, look at the MYSQLi Documentation

Upvotes: 2

Related Questions