views:

191

answers:

1

Hello guys.

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;
     }
    }
+2  A: 
$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

Chacha102
How can i soe after execute make "fetch_object" ? i have problems whiat that :/
NeoNmaN
Wow .. the PHP documentation is extremely confusing...
Chacha102
Chacha: yes my words... :/
NeoNmaN
Well, I gave you the first part, the second part someone else should be able to solve. I'm just getting a headache thinking about it. Its called bad API design.
Chacha102
Tanks Chacha102, you samlpe its relly use ful, bot i online need now to how i can select it out to e.g. foreach( $obj AS $value ) right now. if you can help me here i accept you answer :)
NeoNmaN
tanks a lot. :)
NeoNmaN