Solved

Test Connection to SQL using PHP

Posted on 2011-03-23
3
275 Views
Last Modified: 2013-12-13
I have this code to test the connection, and I get Failure every time.
1.  Please see if I am missing anything in the PHP codes.
2.  Also, is there a different way to connect to "SQL server" VS "MYSQL"? because I see "mysql_connect" in these code. or it is the same approach for both?


<?php 


$dbuser="USER";
$dbpass="PASS";
$dbname="DBname";  //the name of the database
$chandle = mysql_connect("localhost", $dbuser, $dbpass) 
    or die("Connection Failure to Database");
echo "Connected to database server<br>";
mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found." . $dbuser);
echo "Database " .  $database . " is selected";
mysql_close($chandle);



?>

Open in new window

0
Comment
Question by:Angha110
3 Comments
 
LVL 5

Accepted Solution

by:
tsmgeek earned 500 total points
ID: 35201354
first are you sure that mysql client modules have been installed?

second, use the code below as it will give you proper errors to screen

$dbuser="USER";
$dbpass="PASS";
$dbname="DBname";  //the name of the database

$link = mysql_connect('localhost', $dbuser, $dbpass);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}else{
    echo 'Connected successfully';
    $db_selected = mysql_select_db($dbname, $link);
    if (!$db_selected) {
        die ('Can\'t use foo : ' . mysql_error());
    }else{
        echo "Database " .  $dbname. " is selected";
    }
}

mysql_close($link);

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35201593
What environment do you use ? Apache / IIS + MySQL + PHP?
If you want to be sure the above combination works, install all-in-one package (WAMP / XAMPP),
the code seems to be ok, add this line to the top of the script:
error_reporting(E_ALL);
0
 

Author Comment

by:Angha110
ID: 35203428
Thank you for responding. My error was the database name. Silly error.

Thank you again,
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question