Jump to content

Proper use of prepare()?


Pancake

Recommended Posts

Just wondering if this is used properly:

 

class sendQuery extends sqlStuff {
    /* Sends the data to the DB */
    function insertInfo($b, $c) {
        $query = $this->cnx->prepare("INSERT INTO users (user, pass) VALUES (?, ?)"); //Prepares the query...
        $query->bind_param('ss', $b,$c); //Binds the value with the query
        if($query->execute()) {    //Runs the query and checks if it was successful.
            return true;
        } else { //Query was unsuccessful! Sends an error message.
            return false; 
            throw new Exception("ERROR doing query!");
        }
        $query->close();
    }
}
/* END sendQuery Class */

try { //Runs the above classes and checks for errors....
    $cnx = new sqlStuff();
    $do = new sendQuery();
    $do->insertInfo('Username', sha1('Password'));
    echo 'Information Inserted Successfully!';
}catch(Exception $e) {
    //If there are any errors, they will be printed out how we defined them earlier.
    echo 'Caught Exception: '. $e->getMessage();
}

 

Will the string be escaped? Or should I do something like mysqli_relal_escape_string() along with it?

 

Note: The sqlStuff just connects and isn't vital to the script.

 

And is it possible to simply do:

new sqlStuff();

 

instead of

$sql = new sqlStuff();

(sqlStuff only has a __construct and __destruct function that doesn't return anything)

Link to comment
Share on other sites

sqlStuff Class:

class sqlStuff {
    /* Creates variables to be used in the SQL Connection. These should be changed to match your SQL server settings */
    protected $db_host = 'localhost';
    protected $db_user = 'root';
    protected $db_pass = 'root';
    protected $db_name = 'database';
    
    /* This one needs to be used accross all classes, so this is public */
    public $cnx;
    
    /* Actually connect to the DB */
    function __construct() {
        if(!$this->cnx = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name)) {
            //A connection was unsuccessfully made...
            return false;
            throw new Exception("ERROR Connecting to DB! Bad username/password?");
                //Makes an error message to be cleaned up later.
        } else {    
            //Connection was successfully made!
            return true;
        }
    }
    
    function __destruct() { //Closes the connection when an unset() function is used.
        $this->cnx->close();
    }
}

 

I suppose I would possible want to use in some procedural coding later:

mysqli_query($sql->cnx, "SELECT * FROM someTable");

 

So it could be some use/

 

And at the end of my script, I use:

 

unset($sql);

 

Whole Script: http://www.newerth.com/pancake/showproject/sqlClassExample

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.