Page 1 of 1
Couldn't connect to server
Posted: Sat Nov 27, 2010 4:52 am
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" ?
Re: Couldn't connect to server
Posted: Sat Nov 27, 2010 12:30 pm
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.
Re: Couldn't connect to server
Posted: Sat Nov 27, 2010 8:00 pm
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.
Re: Couldn't connect to server
Posted: Sat Nov 27, 2010 8:32 pm
by McInfo
I don't understand the question. Hasn't the database been created already?
Re: Couldn't connect to server
Posted: Sun Nov 28, 2010 12:41 am
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 ?
Re: Couldn't connect to server
Posted: Sun Nov 28, 2010 1:43 am
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.
Re: Couldn't connect to server
Posted: Sun Nov 28, 2010 6:03 am
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' ?
Re: Couldn't connect to server
Posted: Sun Nov 28, 2010 1:26 pm
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.
Re: Couldn't connect to server
Posted: Mon Nov 29, 2010 6:08 am
by phpBrandNew
Ya, I really don't understand.
What script should I add in order to have the table appear on the page ?
Re: Couldn't connect to server
Posted: Mon Nov 29, 2010 1:55 pm
by McInfo
Re: Couldn't connect to server
Posted: Mon Nov 29, 2010 7:38 pm
by phpBrandNew
Thanks for the info.