Couldn't connect to server

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
phpBrandNew
Forum Commoner
Posts: 36
Joined: Thu Sep 02, 2010 12:51 am

Couldn't connect to server

Post by phpBrandNew »

The following is my code:

<?php
$users="root";
$host="localhost";
$password="";
$database="Animal";
$connection=mysql_connect($host,$users,$password)
or die("Couldn't connect to server");
$db=mysql_select_db($database,$connection)
or die("Couldn't connect to server");
?>

The result is "Couldn't connect to server".
What are the possible reasons that cause "Couldn't connect to server" ?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Couldn't connect to server

Post by McInfo »

Use different error messages at different error points. It is difficult to isolate the problem if all the error messages are identical.

Also, mysql_error() and mysql_errno() will return more useful information than a generic "Couldn't connect to server" message.
phpBrandNew
Forum Commoner
Posts: 36
Joined: Thu Sep 02, 2010 12:51 am

Re: Couldn't connect to server

Post by phpBrandNew »

Where should I place the database (or server) ? I use MySQL 5.1 Command line client to create database and I use XAMPP.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Couldn't connect to server

Post by McInfo »

I don't understand the question. Hasn't the database been created already?
phpBrandNew
Forum Commoner
Posts: 36
Joined: Thu Sep 02, 2010 12:51 am

Re: Couldn't connect to server

Post by phpBrandNew »

The database has been created using MySQL command line client, let say database 'Animal'. If I want connect to this database, am I using the same way as I posted ?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Couldn't connect to server

Post by McInfo »

The script appears to be correct. Unfortunately, it is impossible to tell which of the two MySQL functions is failing because the two error messages are identical. That is why I hinted that you should replace the "Couldn't connect to server" strings with calls to mysql_error(), which will return a different error message for a server connection failure than for a database selection failure.
phpBrandNew
Forum Commoner
Posts: 36
Joined: Thu Sep 02, 2010 12:51 am

Re: Couldn't connect to server

Post by phpBrandNew »

I change the code as follow:

<?php
$users="root";
$host="localhost";
$password="";
$database="team";
$connection="mysql_connect($host,$users,$password)
or die('Couldn't connect to server')";
$result = "mysql_query
('SELECT * FROM members ORDER BY id') or die('error querying database')";
?>

This time I see blank in the browser. Why I couldn't see the table 'members' ?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Couldn't connect to server

Post by McInfo »

Please use BBCode tags so your code will appear in a box like this:

Code: Select all

<?php
$users = "root";
$host = "localhost";
$password = "";
$database = "team";
$connection = "mysql_connect($host,$users,$password) or die('Couldn't connect to server')";
$result = "mysql_query('SELECT * FROM members ORDER BY id') or die('error querying database')";
The script creates six variables. All of them hold strings. There are no function calls. Unused variables equals blank page.

I think you are ignoring what I am telling you or you do not understand.
phpBrandNew
Forum Commoner
Posts: 36
Joined: Thu Sep 02, 2010 12:51 am

Re: Couldn't connect to server

Post by phpBrandNew »

Ya, I really don't understand.
What script should I add in order to have the table appear on the page ?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Couldn't connect to server

Post by McInfo »

PHP Manual:
phpBrandNew
Forum Commoner
Posts: 36
Joined: Thu Sep 02, 2010 12:51 am

Re: Couldn't connect to server

Post by phpBrandNew »

Thanks for the info.
Post Reply