TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   mysqli OOP interface introduction (http://www.talkphp.com/general/1795-mysqli-oop-interface-introduction.html)

Matt83 12-22-2007 03:03 PM

mysqli OOP interface introduction
 
When PHP 5 and MySQL 4.1 came by, there where so many new points to include or change in the old ext/mysql that they came to the conclusion that it was better to release a new extension, basically as a way to start over and clean things up. mysqli is the "new" extension provided in PHP5 that its designed to work and support the new features found in Mysql version 4.1.3 or above. It includes SSL connections, stronger password algorithm, prevents SQL injection, makes use of new and more efficient MySQL binary protocol and more. Overall it provides greater speed, better security and new/improved features. One of them that i like its the object-oriented interface. It said that the OOP interface is slightly slower than the procedural interface but its tiny compared for the time it saves when writing code and how much easier to read it makes it.

Mysqli OOP interface sample:

PHP Code:

<?php
# ------------------------------------------------------------
# Simple script to fetch an associative array.
# ------------------------------------------------------------

// Connect To Database
$dbh = new mysqli"host","user","pass","database" ) or die( mysqli_error$dbh ) );

// Query
$res $dbh->query('SELECT * FROM table');

while ( 
$row $res->fetch_assoc() )

      
/* */
}
?>

The example above can of course be achieved with regular mysql functions ( as you may see the code above is pretty similar to the old ext/mysql ) but the mysqli OOP interface is much more cleaner from a visually point of view.

Here you can see a list of methods and properties that can be used with mysqli: PHP: mysqli - Manual

There are a good bunch of other things to mention about mysqli like multiple queries, prepared statements, the support for commit and rollback, etc. Anyways i hope this brought some light on this subject as i believe everyone should be encouraged to start using this extension.

Cheers

Orc 12-22-2007 05:24 PM

Interesting, this seems to be a whole lot simpler than generally connecting to MySQL, which you must insert "mysql_select_db" for selection of the database, a lot simpler! Thanks.

xenon 12-22-2007 08:00 PM

woot?!

Quote:

Originally Posted by Matt83
mysqli is the "new" extension provided in PHP5

mysqli has been a PHP extension since PHP4.

Matt83 12-22-2007 08:10 PM

Quote:

Originally Posted by xenon (Post 7093)
woot?!



mysqli has been a PHP extension since PHP4.

yes, hence the quotes around it.. "new"

bdm 12-22-2007 11:52 PM

It's also worth mentioning that mysqli will only protect against SQL injection if you use prepared statements.

I was going to write a tutorial about the basics of using the mysqli extension. But why write one when Zend has already done an outstanding job?

Part I - Overview and Prepared Statements
Part II - Extending mysqli


All times are GMT. The time now is 12:36 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0