<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "databasename";
// Buat Koneksi
$conn = new mysqli($servername, $username, $password, $dbname);
// Periksa Koneksi
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "Insert Into Person (firstname, lastname, email) Values ('Mike', 'Lie', mike@yahoo.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
2. MySQLi Prosedural
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "databasename";
// Buat Koneksi
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Periksa Koneksi
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "Insert Into Person (firstname, lastname, email) Values ('Mike', 'Lie', mike@yahoo.com')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
3. PDO
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "databasename";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// atur mode error PDO ke exceptions
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "Insert Into Person (firstname, lastname, email) Values ('Mike', 'Lie', mike@yahoo.com')";// gunakan exec() hasil tidak dikembalikan
$conn->exec($sql);
echo "New record created successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>