Solved

Test Connection to SQL using PHP

Posted on 2011-03-23
3
276 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

730 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