Nobodyknows2202 Nobodyknows2202 - 1 year ago 52
SQL Question

How do I set up PHP and MySQL?

Here is my problem. I am trying to create and host a website, and it is going to need to include a database. I've set up Apache2, and it works fine. I've set up a MySQL server, and as far as I know, it works fine. I just can't get a PHP file to connect to the database, or show anything! I'm running a Linux distro and I just need some basic PHP code to connect to my database, or I need someone to correct my code:

$dbhost = "localhost";
$dbuser = "userweb";
$dbpass = "lightsaber";

echo "Hello!";

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

echo "Hello!";

if(! $conn ) {
echo "It doesn't work!";
} else {
echo "It works!";

$sql = 'SELECT * FROM Joe';
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
die('Could not get data: ' . mysql_error());

while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Fighter ID :{$row['fighter_id']}";

echo "Fetched data successfully\n";


This code only display "Hello!" once! I just don't understand how PHP and MySQL communicate.

Thank you in advance!

EDIT: I have php5.

Answer Source

I believe you have to install php separatly either as a module of apache or set it up yourself.

You need to insert the following lines into your Apache httpd.conf configuration file to load the PHP module for Apache 2.x:

Example #1 PHP and Apache 2.x as handler

LoadModule php5_module "c:/php/php5apache2.dll"
AddHandler application/x-httpd-php.php

#configure the path to php.ini
PHPIniDir "C:/php"

I refer you to: