Back to PHP

See Also MySQLOracleDocker_PHP

PHP Database Extensions

1. MySQL Drivers and Plugins

http://php.net/manual/zh/set.mysqlinfo.php

1.1. Mysql — 原生 MySQL API

http://php.net/manual/zh/mysql.examples-basic.php

<html><head><title>MySQL Test</title> </head> <body>
<?php
  echo "<p>Native MySQL API Test</p>";
  // Connecting, selecting database
  $link = mysql_connect("192.168.1.111", "php", "php") //ip addr show en0
    or die("<p>Could not connect: ". mysql_error()."</p>");
  echo "<p>Connected successfully</p>";
  mysql_select_db("test") or die("<p>Could not select database</p>");

  // Performing SQL query
  $query = "SELECT now() AS time FROM dual";
  $result = mysql_query($query) or die("<p>Query failed: " . mysql_error()."</p>");

  // Printing results in HTML
  echo "<table>\n";
  while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
      echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
  }
  echo "</table>\n";

  // Free resultset
  mysql_free_result($result);

  // Closing connection
  mysql_close($link);
?>
</body></html>

1.2. Mysqlnd — MySQL Native Driver

http://php.net/manual/zh/book.mysqlnd.php: MySQL Native Driver is a replacement for the MySQL Client Library (libmysqlclient). MySQL Native Driver is part of the official PHP sources as of PHP 5.3.0.

1.3. Mysqli — MySQL增强版扩展

http://php.net/manual/zh/mysqli.query.php & MySQLi Samples

<html><head><title>MySQL Test</title> </head> <body>
<?php
  echo "<p>MySQLi API Test</p>";
  $servername = "192.168.1.111"; //ip addr show en0
  $username = "php";
  $password = "php";
  $dbname = "test";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }


  // Performing SQL query
  $sql = "SELECT now() AS time FROM dual";
  $result = $conn->query($sql) or die("<p>Query failed: " . mysql_error()."</p>");

  // Printing results in HTML
  if ($result->num_rows > 0) {
    echo "<table>\n";
    // output data of each row
    while($row = $result->fetch_assoc()) {
      echo "\t<tr>\n";
      echo "\t\t<td>time</td><td>" . $row["time"]. "</td>\n";
      echo "\t</tr>\n";
    }
    echo "</table>\n";
  } else {
    echo "0 results";
  }
  $conn->close();
?>
</body></html>

2. Oracle Drivers and Plugins

2.1. OCI8 — Oracle OCI8

http://php.net/manual/en/book.oci8.php

2.1.1. Installation

http://php.net/manual/en/oci8.installation.php

pecl install oci8

<!DOCTYPE html>
<html><head><title>PHP Oracle Test</title> </head> <body>
<?php
echo "<p>OCI8 API Test</p>";
$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 111.122.233.0)(PORT = 1521)))(CONNECT_DATA=(SID=RACDB1)))"; 

try {
    $conn = oci_connect('liyan', 'mypasswd', $db);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
    echo $db;
}

oci_close($conn);
?>
</body>
</html>

3. Reference

MainWiki: PHP_Extensions_DB (last edited 2015-07-12 23:59:19 by twotwo)