E: info@globalcodester.com
W: www.globalcodester.com

@globalcodester
Agenda
•

Introduction

•

Setup Connection

•

Query

•

Output query results

•

Number of return rows

•

Number of affected rows

•

Free result

•

Escaping characters

•

Close connection

•

MySQLi Transaction

•

Commit

•

Rollback

•

Final Conclusion
W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
Introduction
•
•
•
•
•
•
•
•
•

A relational database
An improved version of the MySQL
Allows object oriented as well as procedural format.
Support for prepared statements
Support for multiple statements
Support for transactions
Recommend to use MySQLi when dealing with MySQL server versions 4.1.3 and newer
The MySQLi extension was introduced with PHP version 5.0.0.
The MySQL Native Driver was included in PHP version 5.3.0.
W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
Setup Connection
•

Instantiating a new instance of MySQLi

Example:

$db = new mysqli('localhost', 'ROOT_USERNAME’, 'ROOT_PASSWORD', 'DATABASE');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}

W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
Query
Let's go ahead and pull out all of the users from the users table where they have status = ‘active’.
Example:

$sql = “SELECT * FROM `users`

WHERE `status` = ‘active’ “;

if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}

W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
Output query results
To loop through the results and output the username for each row on a new line.
Example:

while($row = $result->fetch_assoc()){
echo $row['username'] . '<br />';
}
OR
while($row = $result->fetch_object()){
echo $row->username . '<br />';
}
As you can see from this, the syntax is not too dissimilar to the old mysql_ syntax that you're probably used to, this
is just better and improved!

W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
Number of return rows
Each mysqli_result object that is returned has a variable defined which is called $num_rows, so all we need to do is
access that variable by doing:
Example:

<?php
echo 'Total results: ' . $result->num_rows;

?>

W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
Number of affected rows
When running an UPDATE query you sometimes want to know how many rows have been updated, or deleted if
running a DELETE query, this is a variable which is inside the mysqli object.
Example:

<?php
echo 'Total rows updated: ' . $db->affected_rows;
?>

W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
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:
Example:

$result->free();
This will free up some system resources, and is a good practice to get in the habit of doing.

W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
Escaping characters
When inserting data into a database, we'll have been to escape it first, so that single quotes get preceded by a
backslash.

Example:

$db->real_escape_string('This is an unescaped "string"');
However, because this is a commonly used function, there is an alias function that you can use which is shorter and
less to type:
Example:

$db->escape_string('This is an unescape "string"');
This string should now be safer to insert into your database through a query.

W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
Close connection
Don't forget, when you've finished playing with your database to make sure that you close the connection:
Example:

$db->close();

W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
MySQLi Transaction
•
•
•

One of the major improvements that MySQLi brings is the ability to use transactions.
A transaction is a group of queries that execute but 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. If updating fields relies on fields being inserted correctly.

•

Ensure that the database engine that you're using supports transactions.

COMMIT and ROLLBACK:
These two keywords Commit and Rollback are mainly used for MySQL Transactions.

•

When a successful transaction is completed, the COMMIT command should be issued so that the changes to all
involved tables will take effect.

•

If a failure occurs, a ROLLBACK command should be issued to return every table referenced in the transaction
to its previous state.
W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
Commit
Disable auto commit:
Firstly you need to make it so that any query you submit doesn't automatically commit in the database.
It's a simple one line boolean value:

$db->autocommit(FALSE);

Commit the queries:
After a few queries that you've ran using $db->query() we can call a simple function to commit the transaction:

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

W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
Rollback
Just as easy as it is to commit something, it's just as simple to roll something back:

$db->rollback();

W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
Final Conclusion
•

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 the new forms of database interaction - MySQLi.

•

You'll make better and secure code.

W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester
Shoot your Questions

W: www.globalcodester.com

E: info@globalcodester.com

@globalcodester

MySQLi - An Improved Extension of MySQL

  • 1.
  • 2.
    Agenda • Introduction • Setup Connection • Query • Output queryresults • Number of return rows • Number of affected rows • Free result • Escaping characters • Close connection • MySQLi Transaction • Commit • Rollback • Final Conclusion W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 3.
    Introduction • • • • • • • • • A relational database Animproved version of the MySQL Allows object oriented as well as procedural format. Support for prepared statements Support for multiple statements Support for transactions Recommend to use MySQLi when dealing with MySQL server versions 4.1.3 and newer The MySQLi extension was introduced with PHP version 5.0.0. The MySQL Native Driver was included in PHP version 5.3.0. W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 4.
    Setup Connection • Instantiating anew instance of MySQLi Example: $db = new mysqli('localhost', 'ROOT_USERNAME’, 'ROOT_PASSWORD', 'DATABASE'); if($db->connect_errno > 0){ die('Unable to connect to database [' . $db->connect_error . ']'); } W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 5.
    Query Let's go aheadand pull out all of the users from the users table where they have status = ‘active’. Example: $sql = “SELECT * FROM `users` WHERE `status` = ‘active’ “; if(!$result = $db->query($sql)){ die('There was an error running the query [' . $db->error . ']'); } W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 6.
    Output query results Toloop through the results and output the username for each row on a new line. Example: while($row = $result->fetch_assoc()){ echo $row['username'] . '<br />'; } OR while($row = $result->fetch_object()){ echo $row->username . '<br />'; } As you can see from this, the syntax is not too dissimilar to the old mysql_ syntax that you're probably used to, this is just better and improved! W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 7.
    Number of returnrows Each mysqli_result object that is returned has a variable defined which is called $num_rows, so all we need to do is access that variable by doing: Example: <?php echo 'Total results: ' . $result->num_rows; ?> W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 8.
    Number of affectedrows When running an UPDATE query you sometimes want to know how many rows have been updated, or deleted if running a DELETE query, this is a variable which is inside the mysqli object. Example: <?php echo 'Total rows updated: ' . $db->affected_rows; ?> W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 9.
    Free result It's advisableto 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: Example: $result->free(); This will free up some system resources, and is a good practice to get in the habit of doing. W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 10.
    Escaping characters When insertingdata into a database, we'll have been to escape it first, so that single quotes get preceded by a backslash. Example: $db->real_escape_string('This is an unescaped "string"'); However, because this is a commonly used function, there is an alias function that you can use which is shorter and less to type: Example: $db->escape_string('This is an unescape "string"'); This string should now be safer to insert into your database through a query. W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 11.
    Close connection Don't forget,when you've finished playing with your database to make sure that you close the connection: Example: $db->close(); W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 12.
    MySQLi Transaction • • • One ofthe major improvements that MySQLi brings is the ability to use transactions. A transaction is a group of queries that execute but 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. If updating fields relies on fields being inserted correctly. • Ensure that the database engine that you're using supports transactions. COMMIT and ROLLBACK: These two keywords Commit and Rollback are mainly used for MySQL Transactions. • When a successful transaction is completed, the COMMIT command should be issued so that the changes to all involved tables will take effect. • If a failure occurs, a ROLLBACK command should be issued to return every table referenced in the transaction to its previous state. W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 13.
    Commit Disable auto commit: Firstlyyou need to make it so that any query you submit doesn't automatically commit in the database. It's a simple one line boolean value: $db->autocommit(FALSE); Commit the queries: After a few queries that you've ran using $db->query() we can call a simple function to commit the transaction: $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. W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 14.
    Rollback Just as easyas it is to commit something, it's just as simple to roll something back: $db->rollback(); W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 15.
    Final Conclusion • 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 the new forms of database interaction - MySQLi. • You'll make better and secure code. W: www.globalcodester.com E: info@globalcodester.com @globalcodester
  • 16.
    Shoot your Questions W:www.globalcodester.com E: info@globalcodester.com @globalcodester