PHP Programming
MySQL

What do you need to do to make PHP and MySQL work together?

Answer

User Avatar
Wiki User
04/07/2009

The easiest way to get started is to install XAMPP. It comes with MySQL, PHP, Apache, phpMyAdmin (for database management,) and various other tools. To connect from PHP to MySQL database, you use this:

$DBconnect = mysql_connect ("localhost","username","password");

mysql_select_db("yourDatabaseName", $DBconnect);

?> That will just connect you ready to use the database, create your tables in phpMyAdmin, it's easier. When you are ready to start adding/viewing data, you could use the following to add data:

$sql = "INSERT INTO myTable VALUES('value1', 'value2')";

mysql_query($sql);

?> Then the following to view data:

$sql = "SELECT * FROM myTable";

$queryResult = mysql_query($sql);

if (mysql_num_rows($queryResult) == 0){

echo "Table Empty";

} else {

while ($dbRecord=mysql_fetch_array($queryResult)){

$value1 = $dbRecord['field1'];

$value2 = $dbRecord['field2'];

echo $value1.', '.$value2.'

';

}

}

}

?>

This is a solution and works fine, but if your looking to install it all on your own/manualy. You have to install MYSQL php module.

Here is a very nice TUT that will help you if you chose to do so manually: http://www.php-mysql-tutorial.com/install-apache-php-mysql.php Every server I've setup I do it manually. Its just the way I am, I like having control over the whole process.