1

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;
        }
    }
1
  • You need to comment your code... Aug 7, 2009 at 19:22

1 Answer 1

2
$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

3
  • How can i soe after execute make "fetch_object" ? i have problems whiat that :/ Aug 7, 2009 at 19:31
  • 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. Aug 7, 2009 at 19:49
  • 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 :) Aug 7, 2009 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.