3

I have an include file for my database connections that has the following code:

$my_db_link = mysql_connect('my_host', 'my_username', 'my_password');
if (!$my_db_link) {
    die('Could not connect: ' . mysql_error());
}

$my_db_name = 'my_database';
mysql_select_db($my_db_name);

Will it make a difference if I use include_once() rather than include(), or require_once() rather than require()? We had a recent "too many connections" error ("PHP Warning: mysql_connect() [function.mysql-connect]: Too many connections ") and I'm wondering if using include() created more connections than necessary.

include_once('my_db_connect.php');

instead of

include('my_db_connect.php');
5

4 Answers 4

1

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

The manual for require_once is here

The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.

Using persistent connections can require a bit of tuning of your Apache and MySQL configurations to ensure that you do not exceed the number of connections allowed by MySQL.

So its better to use mysql_pconnect() to prevent over loading of connections. Check the manual here

1

Will it make a difference if I use include_once() rather than include(), or require_once() rather than require()?

In this circumstance, no. However, in higher-performance applications, be careful about using include_once/require_once on some systems. PHP uses a stat() call every time the *_once() function is used in your code to determine if it has included that file already or not. If you are careful with your design, you'll avoid double-includes entirely; most higher-level PHP developers avoid it by using object-oriented programming mixed with just-in-time autoloading of class files.

edit: http://www.techyouruniverse.com/software/php-performance-tip-require-versus-require_once

Something to chew on for the bored. :)

0

If you are using mod_php, consider using mysql_pconnect() for connection pooling. More on connection persistence here:

http://www.php.net/manual/en/features.persistent-connections.php

0

Using include_once instead of include will only avoid another include of the same file within the same runtime. Besides that, mysql_connect will already use already an existing connection if there is one with the same credentials.

But you could try mysql_pconnect for persistent connections:

mysql_pconnect() acts very much like mysql_connect() with two major differences.

First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.