Chris Muench
Chris Muench

Reputation: 18318

Can too many connections happen in this php script? (mysql_select_db)

The script below is used for updating schema of 100 databases.

I am making one call to mysql_connect, does mysql_select_db cause another connection to be made or am I ok? (I run this script to update the schema of 100 or so database)

$conn = mysql_connect("localhost", "root", "PASSWORD");
$show_db_query = mysql_query('SHOW databases');
$databases = array();
while ($row = mysql_fetch_assoc($show_db_query)) 
{ 
    if (!in_array($row['Database'], $exclude_dbs))
    {
        $databases[] = $row['Database'];
    }
}

foreach($databases as $database)
{
    mysql_select_db($database, $conn);
    echo "Running queries on $database\n***********************************\n";
    foreach($sql_queries as $query)
    {
        if (!empty($query))
        {
            echo "$query;";
            if (!mysql_query($query))
            {
                echo "\n\nERROR: ".mysql_error()."\n\n";
            }
        }
    }
    echo "\n\n";
}

Upvotes: 1

Views: 109

Answers (1)

Shakti Singh
Shakti Singh

Reputation: 86406

As long as mysql_select_db found a last connection or a connection identifer is supplied with the calling statment, it does not create a new connection.

Since you are passing a connection identifer so it does not create a new connection to mysql. you are safe to go in this case of multiple connection but be aware that mysql extension is no longer maintained, you could try mysqli or PDO.

Upvotes: 1

Related Questions