Solved

Test Connection to SQL using PHP

Posted on 2011-03-23
3
272 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for responding. My error was the database name. Silly error.

Thank you again,
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now