I am trying to build a small website using PHP and MySQL serve as the database. Can anyone tell me how to connect to MySQL database using PHP and is it similar to using Access because I have succeeded in doing that. Thanks

$host = 'localhost';
$user = 'DatabaseUsername';
$pass = 'DatabasePassword';
$dbName = 'DatabaseName';

$connection = mysql_connect($host, $user, $pass) or die("could not connect to the database " . mysql_error());

$db = mysql_select_db($dbName) or die("could not select database " . mysql_error());

$query = 'what ever you sql query is';

reseult = mysql_query($query, $connection) or die("could not execute query " . mysql_error());

that should help

Hey.

I recommend you try the improved MySQL extension, rather then the old MySQL functions.

<?php
$dbLink = new mysqli("host", "user", "password", "database");
if(mysqli_connect_errno()) {
	die("MySQL connection failed: ". mysqli_connect_error());
}

$result = $dbLink->query("SELECT stuff");
while($row = $result->fetch_row()) {
	// etc...
}
@$result->close();

$dbLink->close();
?>

Or, if you aren't a fan of OOP:

<?php
$dbLink = mysqli_connect("host", "user", "password", "database");
if(mysqli_connect_errno()) {
	die("MySQL connection failed: ". mysqli_connect_error());
}

$result = mysqli_query($dbLink, "SELECT stuff");
while($row = mysqli_fetch_row($result)) {
	// etc...
}
@mysqli_free_result($result);

mysqli_close($dbLink);
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.