tags:

views:

44

answers:

2

database get connected successfully....but... here is my code

<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'databasename';

mysql_connect($host, $user, $pass) or die ("Database Not Connected");
mysql_select_db($db) or die ("Database Not Fount");

?>

but the database is regularly disconnecting and connecting after 30-40 minutes....please help me, that what's going on.....

+1  A: 

This may be the problem of confusing variables. Your connection and db-selection shouldn't be confused with queries that will run at a later time.

$conn = mysql_connect($host, $user, $pass) or die(mysql_error());
        mysql_select_db($db) or die(mysql_error());

$query = "SELECT id, username FROM users";

In this example, $conn will not be used to reference anything other than my resource. My query, ran at a later time, will be known as $query, so as to not confuse myself.

I would also suggest watching the execution times of your queries, and the number of concurrent connections opened. If you need to, be sure to close your connections:

mysql_close($conn); // note the importance of a unique variable here
Jonathan Sampson
+3  A: 

Don't forget to close your connection with mysql_close().

Too many connections will cause problems so that might explain your disconnects.

andyuk
do i need to close the connection after every query.......??
Web Worm
At the end of your script, testkhan.
Jonathan Sampson
From the manual ( http://php.net/manual/en/function.mysql-close.php ): Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.
naivists