$conn = new mysqli code example
Example 1: how to connect pdo php database
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Example 2: mysqli database
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
if (mysqli_connect_errno()) {
printf("Échec de la connexion : %s\n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("La base de données courante est %s.\n", $row[0]);
$result->close();
}
$mysqli->select_db("world");
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("La base de données courante est %s.\n", $row[0]);
$result->close();
}
$mysqli->close();
?>