0

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

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Atli
0
$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

Edited by leviathan185: tags

1

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);
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.