How Different Ways of Working with PHP and MySQL?

MySQL Examples in Both MySQLi and PDO Syntax 
In this, and in the following chapters we demonstrate three ways of working with PHP and MySQL:
 MySQLi (object-oriented)
 MySQLi (procedural)
 PDO 
Open a Connection to MySQL
Before we can access data in the MySQL database, we need to be able to connect to the server:



Example (MySQLi Object-Oriented)
 <?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {   
die("Connection failed: " . $conn->connect_error);
}
  echo "Connected successfully";
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {   
echo "Database created successfully";
}
else
{   
echo "Error creating database: " . $conn->error;
}
 $conn->close();
?>
Tips: If you have to use a specific port, add an empty string for the database-name argument, like this: new mysqli("localhost", "username", "password", "", port)





Post a Comment

1 Comments