Solved

Test Connection to SQL using PHP

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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 …
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

776 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