0

I am evaluating MySQL governor on a cloudlinux environment (CentOS and Cpanel are also installed) This is a test server so no actual database connections are running.

I want to be able to restrict the nr of database user connections.

I have set a limit in MySQL governor but I am unable to test it. I need some sort of script (php?) that can open up a bunch of MySQL connections to be able to see some result.

Here is a sample code I am trying to run, that is supposed to generate multiple connections, but it only opens one.

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

// Create connection

while(1) {
#$conn = new mysqli($servername, $username, $password, $dbname);
$conn = mysql_pconnect($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
}

?>

Thanks

anarchist
  • 383
  • 2
  • 5
  • 18
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You're using a mix of `mysql_` and `mysqli_` functions. – Jay Blanchard Jan 05 '15 at 14:36
  • Or if you want/have to use mysql functions take a look at this answer here: http://stackoverflow.com/questions/274892/how-do-you-connect-to-multiple-mysql-databases-on-a-single-webpage You have to pass TRUE for the fourth parameter to mysql_connect() - (new_link). Otherwise the same connection is reused. – Whirlwind Jan 05 '15 at 15:18
  • Ok thanks, then I stick with $conn = new mysqli($servername, $username, $password, $dbname); – anarchist Jan 05 '15 at 15:28

1 Answers1

0

Perhaps something like this? A simple loop, most probably I may have not understood your question well though. You can connect multiple times using a simple simple loop.

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

// Create connection
$i = 0;
while($i<4) { //here loop untill 4..
$i++;
echo $i; //just to echo the number... 
$conn = new mysqli($servername, $username, $password, $dbname);
//$conn = mysql_pconnect($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else{
    echo 'Success';
    }
}

?>
Arfan
  • 17
  • 6