2

I have a PHP Code to insert some information from a large text file in the beginning of the file i have this code:

<?php
$host = "localhost";
$username = "user";
$password = "pass";
$db = "dbname";

mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_close;

set_time_limit(0);

$fh = fopen("file.txt", "r");
while ($row = fgets($fh)) {
//// CODE ....
   mysql_query("INSERT IGNORE INTO `TABLE` VALUES (NULL, '$ETC', '$ETC', '$ETC')"); 
}
    fclose($fh);
?>

but after some iserts i got this error :

Warning: mysql_connect(): User co_p already has more than 'max_user_connections' active connections in /home/username/public_html/file.php on line 7 User co_p already has more than 'max_user_connections' active connections

any solution for this problem ?

  • I'm the OWNER of the SERVER !
Jose Luis
  • 63
  • 1
  • 10
  • 4
    FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde Jul 17 '17 at 11:47
  • Sounds like you either need to optimize your resource management or find a host with less restrictive rules around open connections. – John Conde Jul 17 '17 at 11:47
  • @JohnConde im the owner of the server – Jose Luis Jul 17 '17 at 11:49
  • Are you connecting before attempting each query? Show more code – RiggsFolly Jul 17 '17 at 11:50
  • @RiggsFolly code edited – Jose Luis Jul 17 '17 at 11:57
  • Since you own the server have you considered opening up the MySQL config files and editing `max_user_connections`? – Jay Blanchard Jul 24 '17 at 16:49

2 Answers2

1
mysql_close; 

should be generating an error.

This command should be

mysql_close();

Add error reporting to the top of your file(s) while testing right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); to see if it yields anything.

But I have to ask, why are you connecting to the database and then closing the connection straight after?

After more code added.

As you are actually trying to access the database, you dont want to do a mysql_close(); where you have coded it.

But I have to add:

Every time you use the mysql_ database extension in new code this happens it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions and prepared statements. Start here

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • im new in php and mysql so i will start learning PDO and MSQLi soon, so should i remove the mysql_close(); from the code ? – Jose Luis Jul 17 '17 at 12:06
  • Yes, at least remove the ones you have directly after the connection run before you use the connection – RiggsFolly Jul 17 '17 at 12:07
0

Try to close mysql connection:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
....
mysql_close($link);