Friday, November 12

PHP: How do I access a database?

Before we can do anything with a database, we need to talk to it. And to talk to it,
we must make a database connection. Logical, isn’t it?
Solution
Here’s how we connect to a MySQL database on the localhost:


$dsn = 'mysql:host=localhost;dbname=arabic;';
$user = 'root';
$password = '';
try
{
$dbh = new PDO($dsn, $user, $password);
echo "koneksi sukses";
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}

No comments:

Post a Comment

Pencarian Google