?
Solved

Test Connection to SQL using PHP

Posted on 2011-03-23
3
Medium Priority
?
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
tsmgeek earned 2000 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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

800 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