Difference between revisions of "PHP111"
From mi-linux
Jump to navigationJump to searchLine 12: | Line 12: | ||
([http://stackoverflow.com/questions/2190737/what-is-the-difference-between-mysql-mysqli-and-pdo source]) | ([http://stackoverflow.com/questions/2190737/what-is-the-difference-between-mysql-mysqli-and-pdo source]) | ||
+ | |||
+ | == The code == | ||
+ | |||
+ | <pre> | ||
+ | <?php | ||
+ | |||
+ | // Connect to server/database | ||
+ | $mysqli = mysqli_connect("localhost", "YOUR_LOGIN", "YOUR_PASSWORD", "YOUR_DATABASE"); | ||
+ | if (mysqli_connect_errno($mysqli)) { | ||
+ | echo "Failed to connect to MySQL: " . mysqli_connect_error(); | ||
+ | } | ||
+ | |||
+ | // Run SQL query | ||
+ | $res = mysqli_query($mysqli, "SELECT name, species, age FROM pet"); | ||
+ | |||
+ | // How many rows were returned? | ||
+ | echo("<p>" . mysqli_num_rows($res) . " record(s) were returned...</p>"); | ||
+ | |||
+ | // Loop through resultset and display each field's value | ||
+ | while($row = mysqli_fetch_assoc($res)) { | ||
+ | echo $row['name']. " - ". $row['species'] ."<br>"; | ||
+ | } | ||
+ | |||
+ | ?> | ||
+ | </pre> | ||
+ | |||
+ | == The code explained == | ||
= Ready to try some more PHP/MySQL? = | = Ready to try some more PHP/MySQL? = | ||
A simple example of using a HTML form to capture search requirements and execute a custom query can be found at [[PHP112|PHP112 - Simple HTML/PHP/MySQL example]] | A simple example of using a HTML form to capture search requirements and execute a custom query can be found at [[PHP112|PHP112 - Simple HTML/PHP/MySQL example]] |
Revision as of 14:28, 29 June 2016
Main Page >> Web Application Development >> Workbook >> PHP and Databases
PHP and Databases – Making the Connection
How to connect
There are 3 ways to connect to a database in PHP:
- (DEPRECATED) The mysql functions are procedural and use manual escaping.
- MySQLi is a replacement for the mysql functions, with object-oriented and procedural versions. It has support for prepared statements. We will use this option on this module
- PDO (PHP Data Objects) is a general database abstraction layer with support for MySQL among many other databases. It provides prepared statements, and significant flexibility in how data is returned.
(source)
The code
<?php // Connect to server/database $mysqli = mysqli_connect("localhost", "YOUR_LOGIN", "YOUR_PASSWORD", "YOUR_DATABASE"); if (mysqli_connect_errno($mysqli)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // Run SQL query $res = mysqli_query($mysqli, "SELECT name, species, age FROM pet"); // How many rows were returned? echo("<p>" . mysqli_num_rows($res) . " record(s) were returned...</p>"); // Loop through resultset and display each field's value while($row = mysqli_fetch_assoc($res)) { echo $row['name']. " - ". $row['species'] ."<br>"; } ?>
The code explained
Ready to try some more PHP/MySQL?
A simple example of using a HTML form to capture search requirements and execute a custom query can be found at PHP112 - Simple HTML/PHP/MySQL example