Sid Webber
Sid Webber

Reputation: 23

How would I connect to a mysql database using PHP?

I have read that PHP is obsoleting the MySql functions.

How will we connect to MySql database?

Upvotes: 1

Views: 111

Answers (4)

Bill Karwin
Bill Karwin

Reputation: 562951

Where did you hear that? I think it would be a great idea, but it's news to me.

It's better to use new mysqli('hostname', 'username', 'password', 'database')

Or (even better) use new PDO('mysql:dbname=database;host=hostname', 'username', 'password')

Upvotes: 4

Aaron
Aaron

Reputation: 11693

Using PDO (PHP Data Objects)

example.

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

Upvotes: 1

Alex Turpin
Alex Turpin

Reputation: 47776

The new, better way of doing it is to use PDO. You can find pretty of examples on the web if you Google for "php mysql pdo", here is one.

Upvotes: 1

Ry-
Ry-

Reputation: 225281

Using the new MySQLi classes.

Upvotes: 1

Related Questions