Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×

MySQLi - An Improved Extension of MySQL

Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Próximo SlideShare
MySQLi
MySQLi
Cargando en…3
×

Eche un vistazo a continuación

1 de 16 Anuncio

MySQLi - An Improved Extension of MySQL

Descargar para leer sin conexión

Presentation on MySQLi - An Improved Extension of MySQL. MySQLi is a relational database which allows object oriented as well as procedural format. For all developer its recommend to use MySQLi when dealing with MySQL server versions 4.1.3 and newer.

Presentation on MySQLi - An Improved Extension of MySQL. MySQLi is a relational database which allows object oriented as well as procedural format. For all developer its recommend to use MySQLi when dealing with MySQL server versions 4.1.3 and newer.

Anuncio
Anuncio

Más Contenido Relacionado

Presentaciones para usted (20)

Similares a MySQLi - An Improved Extension of MySQL (20)

Anuncio

Más reciente (20)

MySQLi - An Improved Extension of MySQL

  1. 1. E: info@globalcodester.com W: www.globalcodester.com @globalcodester
  2. 2. 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
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 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. 12. 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
  13. 13. 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
  14. 14. 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
  15. 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. 16. Shoot your Questions W: www.globalcodester.com E: info@globalcodester.com @globalcodester

×