PHP 5 and later can work with a MySQL database using: Show
Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012. Should I Use MySQLi or PDO?If you need a short answer, it would be "Whatever you like". Both MySQLi and PDO have their advantages: PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases. So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included. Both are object-oriented, but MySQLi also offers a procedural API. Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very important for web application security. MySQL Examples in Both MySQLi and PDO SyntaxIn this, and in the following chapters we demonstrate three ways of working with PHP and MySQL:
MySQLi InstallationFor Linux and Windows: The MySQLi extension is automatically installed in most cases, when php5 mysql package is installed. For installation details, go to: http://php.net/manual/en/mysqli.installation.php PDO InstallationFor installation details, go to: http://php.net/manual/en/pdo.installation.php Open a Connection to MySQLBefore we can access data in the MySQL database, we need to be able to connect to the server: Example (MySQLi Object-Oriented)<?php // Create connection // Check connection Note on the object-oriented example above: $connect_error was broken until PHP 5.2.9 and 5.3.0. If you need to ensure compatibility with PHP versions prior to 5.2.9 and 5.3.0, use the following code instead: // Check connection Example (MySQLi Procedural)<?php // Create connection // Check connection Example (PDO)<?php try { Note: In the PDO example above we have also specified a database (myDB). PDO require a valid database to connect to. If no database is specified, an exception is thrown. Tip: A great benefit of PDO is that it has an exception class to handle any problems that may occur in our database queries. If an exception is thrown within the try{ } block, the script stops executing and flows directly to the first catch(){ } block. Close the ConnectionThe connection will be closed automatically when the script ends. To close the connection before, use the following: MySQLi Object-Oriented:$conn->close(); MySQLi Procedural:mysqli_close($conn); AJAX can be used for interactive communication with a database. AJAX Database ExampleThe following example will demonstrate how a web page can fetch information from a database with AJAX: ExamplePerson info will be listed here... Example Explained - The MySQL DatabaseThe database table we use in the example above looks like this:
Example ExplainedIn the example above, when a user selects a person in the dropdown list above, a function called "showUser()" is executed. The function is triggered by the onchange event. Here is the HTML code: Example<html> <form> </body> Run example » Code explanation: First, check if person is selected. If no person is selected (str == ""), clear the content of txtHint and exit the function. If a person is selected, do the following:
The PHP FileThe page on the server called by the JavaScript above is a PHP file called "getuser.php". The source code in "getuser.php" runs a query against a MySQL database, and returns the result in an HTML table: <!DOCTYPE html> table, td, th { th {text-align: left;} <?php $con = mysqli_connect('localhost','peter','abc123'); mysqli_select_db($con,"ajax_demo"); echo "<table> Explanation: When the query is sent from the JavaScript to the PHP file, the following happens:
|