简体   繁体   中英

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:

<?php
   $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';
   mysql_select_db('SwordofLight');
   $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";

   mysql_close($conn);
?>

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

Thank you in advance!

EDIT: I have php5.

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: http://php.net/manual/en/install.windows.apache2.php

I think there is a wrong in MySQL connection.

try this. this might help.

mysqli_connect($host, $user, $password, $database);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM