Monday, October 8, 2012

Test MySQL connection in php using xampp




XAMPP is a very easy to install Apache Distribution for Linux, Solaris, Windows and Mac OS X. The package includes the Apache web server, MySQL, PHP, Perl, a FTP server and phpMyAdmin.
XAMPP Web Site

Go to http://localhost/phpmyadmin/ after installing xampp
create database "test" under Database tab. Under "Users", check database username "root", host "localhost", password "no" for default

My default working directory is: C:\xampp\htdocs\xampp\
 Create a directory phptest, input the following script in test.php:
<?php
  
$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "test";

if(mysql_connect("$db_host","$db_username","$db_pass"))
{echo "connect to MySQL server ".$db_host." with username ".$db_username."<br />";}
else die ("could not connect to mysql");
if(mysql_select_db("$db_name"))
{echo "connect to dtabase ".$db_name;}
else die("no database");            
?>

Run
http://localhost/xampp/phptest/test.php

If everything is OK, the result should return:
connect to MySQL server localhost with username root
connect to dtabase test

No comments:

Post a Comment