'MySQLi' For Beginners - Codular
'MySQLi' For Beginners - Codular
Home Discussions
Any of the functions that are prefixed with mysql_ are now being
discouraged by PHP themselves as visible on this doc page, instead
you should look to use one of the following:
Each has its advantages, PDO for example will work with various
different database systems, where as MySQLi will only work with
MySQL databases. Both are object oriented, but MySQLi allows
http://codular.com/php-mysqli 1/8
10/25/2014 'MySQLi' for Beginners - Codular
PHP MySQLi
Here we'll mostly be looking at the object oriented implementation,
however, there is no reason you can't use this in a procedural format,
but again no reason you shouldn't use the OO implementation.
Connecting
Connecting is as simple as just instantiating a new instance of
MySQLi, we'll be using a username of user with a password of pass
Querying
Let's go ahead and pull out all of the users from the users table where
they have live = 1 :
$sql = <<<SQL
SELECT *
FROM `users`
WHERE `live` = 1
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
http://codular.com/php-mysqli 2/8
10/25/2014 'MySQLi' for Beginners - Codular
while($row = $result->fetch_assoc()){
echo $row['username'] . '<br />';
}
As you can see from this, the syntax isn't too dissimilar to the old
mysql_ syntax that you're probably used to, this is just better and
improved!
<?php
echo 'Total results: ' . $result->num_rows;
?>
<?php
echo 'Total rows updated: ' . $db->affected_rows;
?>
http://codular.com/php-mysqli 3/8
10/25/2014 'MySQLi' for Beginners - Codular
Free result
It's advisable to free a result when you've finished playing with the
result set, so in the above example we should put the following code
after our while() loop:
$result->free();
Escaping characters
When inserting data into a database, you'll have been told (I hope) to
escape it first, so that single quotes get preceeded be a backslash.
This will mean that any quotes won't break out of any that you use in
your SQL. This is still the case - and you should look to use the
below method:
This string should now be safer to insert into your database through a
query.
$db->close();
http://codular.com/php-mysqli 4/8
10/25/2014 'MySQLi' for Beginners - Codular
Prepared Statements
Prepared statements are complex to get your head around, but are
really useful and can help alleviate a lot of the potential issues that
you might have with escaping. Prepared statements basically work by
you playing a ? where you want to substitute in a string , integer , blob
or double . Prepared statements don't substitute the value into the SQL
so the issues with SQL injections are mostly removed.
Define a statement
Let's try to grab all of the users from the users table where they have
a username of bob . We'd firstly define the SQL statement that we'd
use:
Bind parameters
We simply use the method bind_param to bind a parameter. You must
specify the type as the first parameter then the variable as the
second - so for instance we'd use s as the first parameter (for
string), and our $name variable as the second:
$name = 'Bob';
$statement->bind_param('s', $name);
http://codular.com/php-mysqli 5/8
10/25/2014 'MySQLi' for Beginners - Codular
No fuss, no mess, just execute the statement so that we can play with
the result:
$statement->execute();
$statement->bind_result($returned_name);
Now we have to actually fetch the results, this is just as simple as the
earlier mysqli requests that we were doing - we'd use the method
fetch() , which returns will assign the returned values into the binded
variables - if we'd binded some.
while($statement->fetch()){
echo $returned_name . '<br />';
}
Close statement
Don't forget to forgo a few seconds of your time to free the result -
keep your code neat, clean and lean:
$statement->free_result();
MySQLi Transactions
One of the major improvements that MySQLi brings is the ability to
use transactions. A transaction is a group of queries that execute but
http://codular.com/php-mysqli 6/8
10/25/2014 'MySQLi' for Beginners - Codular
don't save their effects in the database. The advantage of this is if you
have 4 inserts that all rely on each other, and one fails, you can roll
back the others so that none of the data is inserted, or if updating
fields relies on fields being inserted correctly.
You need to ensure that the database engine that you're using
supports transactions.
$db->autocommit(FALSE);
$db->commit();
Pretty simple stuff so far, and it's meant to be easy and approachable
so that you have no reason to not use it.
Rollback
Just as easy as it is to commit something, it's just as simple to roll
something back:
$db->rollback();
http://codular.com/php-mysqli 7/8
10/25/2014 'MySQLi' for Beginners - Codular
there to be used.
Final Thoughts
Using mysql_ functions is a foolish move to make, don't use these
outdated and useless methods because they're easier, or quicker.
Man up and tackle one of the new forms of database interaction -
MySQLi or PDO - you'll make @mfrost503 happier, and have better
code too.
Tweet 166
Posted 16th July 2012 by Michael
http://codular.com/php-mysqli 8/8