MySQL Database Using PHP (Day-1)
MySQL Database Using PHP (Day-1)
PHP
Outline
1. Connecting to MySQL
2. Executing Queries
3. Example: Adding a Record
4. Retrieving Query Results
PHP and Database
Mysql – popular open-source database
management system
PHP usually works with Mysql for web-based
database applications
XAMP applications—Web-based applications
that use Cross Platform, Apache, Mysql, and
PHP.
Connecting to MySQL
PHP offers three different ways to connect to MySQL
• MySQL
• MySQL I. The i stands for improved.
• PDO : PHP Data Objects
Connecting to MySQL
<?php
//connect to database
$host = "localhost";
$username = "root";
$password = "";
$dbname = "students";
$connection = mysqli_connect($host,$username,$password,$dbname);
if(mysqli_connect_errno()){
die("database connection failed. Error Number:" .
mysqli_connect_errno()." Error Type.".mysqli_connect_error());
}
?>
Connecting to MySQL
• mysqli_connect() connects to a MySQL server running at $host
and selects the database $dbname Upon success, $connection
holds a reference to the database which is needed by
subsequent mysqli_* function calls.