1

I was using following php code to check number of MySQL connections

$connection=mysql_connect("localhost","$mysql_user","$mysql_pwd");
if (mysqli_connect_errno())
  { echo "NO CONNECTION"; }
  else
  {
unset ($result);
$result = mysql_list_processes($connection);
while ($row = mysql_fetch_assoc($result)){
$mysql_data[]=$row["db"];
}
mysql_free_result($result);
$n_connections=count($mysql_data)-1;
}
echo "$n_connections";

Since mysql_connect was deprecated in PHP 5.5.0, I changed $connection with

$connection=mysqli_connect("localhost","$mysql_user","$mysql_pwd", "$mysql_db");

After doing this change, $n_connections is no more returning the correct value , what is wrong in the code please ?

Thank you

note : function mysqli_list_processes in php.net does not help, I tried it and it does not work.

note 2: this post is not a duplicate of "How to change mysql to mysqli?" , my problem is replacing "$result = mysql_list_processes($connection);"

gr68
  • 420
  • 1
  • 10
  • 25
  • you need to change `mysql_*` every-where not in just one line – Alive to die - Anant May 04 '18 at 07:54
  • If I change mysql_list_processes with mysqli_list_processes it returns error. – gr68 May 04 '18 at 07:55
  • There's what looks like useful input here: http://php.net/manual/en/function.mysql-list-processes.php (see the user comment at the bottom). – MandyShaw May 04 '18 at 08:08
  • Possible duplicate of [How to change mysql to mysqli?](https://stackoverflow.com/questions/1390607/how-to-change-mysql-to-mysqli) – Nigel Ren May 04 '18 at 08:12
  • function mysqli_list_processes does not help, I tried it and it does not work. It's not a duplicate of "How to change mysql to mysqli?" , my problem is replacing "$result = mysql_list_processes($connection);" – gr68 May 04 '18 at 08:30

2 Answers2

3

You can simply try this query

show status where `variable_name` = 'Threads_connected';

result from phpmyadmin

n1md7
  • 2,854
  • 1
  • 12
  • 27
0

this should solve the problem and return the correct number of DB connections.

$connection=mysqli_connect("localhost","$mysql_user","$mysql_pwd", "$mysql_db");
if (mysqli_connect_errno())
  { echo "NO CONNECTION"; }
  else
  {

if ($result = mysqli_query($connection, "SHOW FULL PROCESSLIST")) {
    $n_connections=mysqli_num_rows($result);
    mysqli_free_result($result);
 $n_connections=$n_connections-1;
}
}
echo "$n_connections";
gr68
  • 420
  • 1
  • 10
  • 25