Jump to content

Cannot connect to the database


leszekj

Recommended Posts

I have mySql, PHP and the Apache server installed on my computer. The database mySql works OK, the PHP as well but when I want to connect to the database on my local computer it seems not to work.
In my php I have the following code:
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'cze00345';
echo 'check 1';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'test';
echo 'check 2';
mysql_select_db($dbname);
echo 'check 3';
mysql_close($conn);
?>
</body>
The browser displays only "check 1", nothing more, so I suspect that there is something wrong with the "$conn=mysql_connect(..." instruction - it may not connect to the database.
"root" and "cze00345" are the user and password I am using in the "mysql -h localhost -u root -p" instruction while reaching the database from the DOS window.
I am wondering whether there is something more I should do in order to connect to the database via PHP.
Link to comment
https://forums.phpfreaks.com/topic/8952-cannot-connect-to-the-database/
Share on other sites

Do you not get the [b]Error connecting to mysql[/b] message which you has setup when the mysql_conncet function fails.

Is this a local install of Apache/PHP/MySQL on your PC? If so which version of PHP are you using? If its PHP5 did you enable the mysql extension and setup the extension_dir directive too in the php.ini?

Also it would be a good idea too to enable display_errors too. As then if there is an error in your php script PHP will report the errors rather tnan hidding them too.
No, I do not get an error reply. I have all - PHP, mySql and Apache locally installed on my computer. I installed all about two months ago, and the versions are from that time - the version of PHP is 5.
I put these instructions into the php.ini file:
extension=php_mssql.dll
extension=php_msql.dll
extension=php_mysql.dll
and also I set:
display_errors = On
I does not work but I will try to restart the computer, and I will send a message.
Thanks.
The only think that I can see is that you are trying to us mysql connect to connect to $dbname - test before you call that varible. That might be why you stop at check1 and don't go any futher.

Try
[code]
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'cze00345';
$dbname = 'test';

echo 'check 1';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$conn) { echo("ERROR: " . mysql_error() . "\n");    }

echo 'check 2';
mysql_select_db($dbname);

?>
[/code]
[!--quoteo(post=370838:date=May 3 2006, 12:39 PM:name=Leszek J)--][div class=\'quotetop\']QUOTE(Leszek J @ May 3 2006, 12:39 PM) [snapback]370838[/snapback][/div][div class=\'quotemain\'][!--quotec--]
After I restarted the computer the error showed:
Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache Group\Apache2\htdocs\u1.php on line 14
So, still there is something wrong.
[/quote]
If you are getting that error message then either the mysaql extension hasn't been enabled or PHP is unable to enable the mysql extension Please read [a href=\"http://www.phpfreaks.com/forums/index.php?s=&showtopic=87276&view=findpost&p=350975\" target=\"_blank\"]this[/a] thread for a step by step guide to do so.
I still have the problem and I do not have any idea what is wrong with the PHP code that cannot connect to the mySql server on my local computer.
I changed the php.ini file, put the libmysql.dll into the Windows folder, I changed some other things in the php.ini and it does not work!
It may be a small thing, and now I am considering the instructions in the php.ini file - whether they are correct. Should it be (I am using Windows XP):
1. doc_root = "C:\Program Files\Apache Group\Apache2\htdocs"
or
2. doc_root = "C:/Program Files/Apache Group/Apache2/htdocs"
or
3. doc_root = "C:\Program Files\Apache Group\Apache2\htdocs\"
or
4. doc_root = "C:/Program Files/Apache Group/Apache2/htdocs/"
The same with setting other directories:
extension_dir = "c:\php\ext"
extension_path = "c:\php\ext" (should I put this command?)
Another thing is that the error may stick somewhere else. In the PHP configuration or in mySql itself? That is why I am giving here the screen of the result of the phpinfo():
[div align=\"center\"][img src=\"http://www.palacyk.lap.pl/report.gif\" border=\"0\" alt=\"IPB Image\" /][/div]
I have tried to fix this problem all day long today but failed so far. I am still trying. I put my php.ini file [a href=\"http://www.palacyk.lap.pl/php.ini\" target=\"_blank\"]here[/a].
Oh, and I would forget to put the error reports that have appeared after my simple programme:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061) in C:\Program Files\Apache Group\Apache2\htdocs\u1.php on line 14

Warning: mysql_select_db() [function.mysql-select-db]: Can't connect to MySQL server on 'localhost' (10061) in C:\Program Files\Apache Group\Apache2\htdocs\u1.php on line 17

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in C:\Program Files\Apache Group\Apache2\htdocs\u1.php on line 17
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in C:\Program Files\Apache Group\Apache2\htdocs\u1.php on line 19[/quote]
You dont need to setup the doc_root directive in the php.ini.

Now you should only have a directive called extension_dir and not extension_path in your php.ini. And it should be in this format:
[code]extension_dir = "C:/php/ext"[/code]

Now when make chnages to the php.ini save it and then [b]restart[/b] your Apache/IIS web server for the changes to affect.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.