PHP and MySQL
PHP and MySQL
Web Development
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB",
$username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
MySQLi Object-Oriented:
$conn->close();
MySQLi Procedural:
mysqli_close($conn);
PDO:
$conn = null;
Parameter Description
database Required. Specifies the database to select.
connection Optional. Specifies the MySQL connection. If not specified,
the last connection opened by mysql_connect() or
mysql_pconnect() is used.
Description
This language construct is equivalent to exit().
Description
void exit ([ string $status ] )
void exit ( int $status )
Terminates execution of the script.
Parameters
status
If status is a string, this function prints the status just before exiting.
If status is an integer, that value will also be used as the exit status.
Exit statuses should be in the range 0 to 254, the exit status 255 is
reserved by PHP and shall not be used. The status 0 is used to
terminate the program successfully.
PHP and MySQL Web tMyn 24
Development
• A typical web database transaction consists of the
following stages, which are numbered in the Figure 1:
Browser 6
Web Server
5 2
MySQL Server 4
PHP Engine