Contoh Script Koneksi MySQL Dengan PDO di PHP

Posted : 29 Nov 2016 MySQL, PHP Last Updated : 10 Oct 2017
Logo PHP & MySQL
Logo PHP & MySQL

<?php
echo ($_POST[‘nama’]);
echo ($_POST[‘kelas’]);
/*mysql_connect(‘localhost’, ‘root’, ”);*/ /*mysql_connect(‘server’, ‘user’, ‘password’) */
$server = “localhost”;
$username = “root”;
$password = “”;
$dbname = “sekolah”;
/* $conn = new mysqli($server, $username, $password, $dbname);/*use mysqli*/
$conn = new PDO(“mysql:host=$server;dbname=$dbname”, $username, $password);/* use PDO to connect to DB */
$sql = “insert into siswa (nama, kelas) values (‘”.$_POST[‘nama’].”‘, ‘”.$_POST[‘kelas’].”‘)”;/*use mysqli*/
$hasil = $conn->query($sql);/*use mysqli*/
$lihat = $conn->query(“select * from siswa”);
/*mysql_db_query(‘sekolah’, “insert into siswa (nama, kelas) values (‘”.$_POST[‘nama’].”‘, ‘”.$_POST[‘kelas’].”‘)”);*/ /*mysql_db_query(‘db_name’, “value to insert”) */
/*$cek_db = “select * from siswa”;*/
/*$conn->close();/*Close connection after use (mysqli)*/
$conn = NULL; /*close connection in PDO */
?>

Leave a reply