User Tools

Site Tools


mysql:database_api

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
mysql:database_api [2016/10/13 15:51] – created petermysql:database_api [2020/07/15 09:30] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== MySQL - Database API ====== ====== MySQL - Database API ======
 +
 +PHP offers three different APIs to connect to MySQL. 
  
 ===== Choosing an API ===== ===== Choosing an API =====
  
-http://php.net/manual/en/mysqlinfo.api.choosing.php+Comparing the three MySQL APIs. 
 + 
 +<code php> 
 +<?php 
 +// PDO 
 +$pdo = new PDO('mysql:host=example.com;dbname=database', 'user', 'password'); 
 +$statement = $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL"); 
 +$row = $statement->fetch(PDO::FETCH_ASSOC); 
 +echo htmlentities($row['_message']); 
 + 
 +// mysqli 
 +$mysqli = new mysqli("example.com", "user", "password", "database"); 
 +$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL"); 
 +$row = $result->fetch_assoc(); 
 +echo htmlentities($row['_message']); 
 + 
 +// mysql 
 +$c = mysql_connect("example.com", "user", "password"); 
 +mysql_select_db("database"); 
 +$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL"); 
 +$row = mysql_fetch_assoc($result); 
 +echo htmlentities($row['_message']); 
 +?> 
 +</code> 
 + 
 +It is recommended to use either the PDO_MySQL or mysqli extensions It is not recommended to use the old mysql extension for new development, as it was deprecated in PHP 5.5.0 and was removed in PHP 7.  
 + 
 +PDO has the advantage that you only need to learn one PHP API if you need to work with different DBMS in the future. 
 + 
 +MySQLi is more powerful and probably more complex to learn
  
  
Line 9: Line 40:
  
 http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
 +
 +
 +
 +===== References =====
 +
 +http://php.net/manual/en/mysqlinfo.api.choosing.php
 +
mysql/database_api.1476373873.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki