database connection failed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

database connection failed

Post by gurjit »

Hi,

I have mysql 5 installed on a microsoft windows server 2003. I want to connect to this mysql database from my linux server. Both servers are hosted with different companies and reside in different countries.


I am using the following to connect but i get the following error:

Warning: mysql_connect(): Lost connection to MySQL server during query in /var/www/vhosts/domain/httpdocs/dbs_connection.php on line 13
Lost connection to MySQL server during query

This is the code i'm using

Code: Select all

<?php
$hostname = "00.000.00.00";
$database = "mydb";
$username = "root";
$password = "password";
$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db("mydb",$conn);
?>
The Windows server does not have php installed but I believe I dont need this because the call is made from the linux server which has PHP 4 installed.

Any ideas what the problem may be?
giliat
Forum Commoner
Posts: 28
Joined: Sun May 20, 2007 2:00 pm

Connects to Database

Post by giliat »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?PHP
// Connects to  Database 

$connection = mysql_connect("your localhost","usser name","password")
or die ("could not connect to server");
$db = mysql_select_db ("database name",$connection)
or die ("could not select database");
//-- end off database connection prosess
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

try mysql_pconnect() and see if that changes the result.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

I tried mysql_pconnect()

and the following error showed

Warning: mysql_pconnect(): Lost connection to MySQL server during query in /var/www/vhosts/domain/httpdocs/dbs_connection.php on line 14
could not connect to server
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you post the first 20 lines of /var/www/vhosts/domain/httpdocs/dbs_connection.php? If you have connection details (like username and password) start them out.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

I had some code commented out at the top of the page, I have taken that out and run the script again and displayed the full code on the page and error message below.

So the full code taking out the commented code now is

Code: Select all

<?php
$connection = mysql_pconnect("**.***.**.**","********","********") 
or die ("could not connect to server"); 
$db = mysql_select_db ("*********",$connection) 
or die ("could not select database"); 
?>
AND THE ERROR READS

Warning: mysql_pconnect(): Lost connection to MySQL server during query in /var/www/vhosts/httpdocs/dbs_connection.php on line 2
could not connect to server

I'm wondering if this could be something to do with mysql drivers on the windows server. Would I need to make a dnsless odbc connection in windows with a new username and password?

Currently I use root details to try and logon.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

You shouldn' t have problems with the drivers for MySQL if the installation didn' t give any errors. My advice check the buglist for MySQL to see if it' s there and see a solution; check the settings on the linux server (permissions for the file and the setup of the database server).
maani786
Forum Newbie
Posts: 16
Joined: Fri Jul 13, 2007 2:13 am

hi

Post by maani786 »

dear give mysql server listening port along with ip address
default port for mysql is 3306..
i have experienced this error and then i solved it by providing port address

your code will look like this:

Code: Select all

<?php 
$hostname = "00.000.00.00";  
$database = "mydb"; 
$username = "root"; 
$password = "password"; 
$conn = mysql_connect($hostname.":3306", $username, $password) or die(mysql_error()); 
mysql_select_db("mydb",$conn); 
?>
OR

Code: Select all

<?php 
$hostname = "00.000.00.00:3306";  
$database = "mydb"; 
$username = "root"; 
$password = "password"; 
$conn = mysql_connect($hostname, $username, $password) or die(mysql_error()); 
mysql_select_db("mydb",$conn); 
?>
hopefully u will be connected..

----------------
maani janjua
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

Thanks maani786 - I tried both solutions, same error as my first post

Lost connection to MySQL server during query

I personal think its going to be an issue with windows drivers, with linux trying to connect to a windows mysql driver. Works fine with linux to linux machines not a problem.

I'll have to look into it. Any solutions would be helpful.
Post Reply