17 de fev. de 2017

PHP connecting with MySQL

There are 3 ways to connect PHP with MySQL:

  1. PHP's MySQL Extension (old one)
  2. PHP's mysqli Extension (current using)
  3. PHP Data Objects (PDO)
I´ll talk about the second one: mysqli.

mysqli means "MySQL improved extension".

The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:
  • Object-oriented interface
  • Support for Prepared Statements
  • Support for Multiple Statements
  • Support for Transactions
  • Enhanced debugging capabilities
  • Embedded server support
As well as the object-oriented interface the extension also provides a procedural interface.

Procedural interface
 $mysqli = mysqli_connect("example.com", "user", "password", "database");  
 if (mysqli_connect_errno($mysqli)) {  
   echo "Failed to connect to MySQL: " . mysqli_connect_error();  
 }  
 $res = mysqli_query($mysqli, "SELECT 'A world full of ' AS _msg FROM DUAL");  
 $row = mysqli_fetch_assoc($res);  
 echo $row['_msg'];


Object-oriented interface
 $mysqli = new mysqli("example.com", "user", "password", "database");  
 if ($mysqli->connect_errno) {  
   echo "Failed to connect to MySQL: " . $mysqli->connect_error;  
 }  
 $res = $mysqli->query("SELECT 'choices to please everybody.' AS _msg FROM DUAL");  
 $row = $res->fetch_assoc();  
 echo $row['_msg'];  


http://php.net/manual/en/mysqli.quickstart.dual-interface.php

Nenhum comentário: