Jump to content

Recommended Posts

Posted

Hi, and thanks, I really appreciate your time.

 

I have this function to connect to my BD. (funciones.php)

function conectarse() {
    
    $conexion = mysqli_connect("localhost", "user", "pass", "DB") or die("Error de conexion: " . mysqli_connect_error());

    if (!$conexion) {

        return False;
    }

    return $conexion;
}

And have this one to select data on my DB. (funciones.php)

function armame($date, $MarketName, $cod) {
    
    $conexion=conectarse();
    
    switch ($cod) {
        case "24H":
            $date2 = strtotime ( '-24 hour' , strtotime ( $date ) ) ;
            $date2 = date ( 'Y-m-d H:i:s' , $date2 );
            break;
        case "4H":
            $date2 = strtotime ( '-4 hour' , strtotime ( $date ) ) ;
            $date2 = date ( 'Y-m-d H:i:s' , $date2 );
            break;
        case "2H":
            $date2 = strtotime ( '-2 hour' , strtotime ( $date ) ) ;
            $date2 = date ( 'Y-m-d H:i:s' , $date2 );
            break;
        case "1H":
            $date2 = strtotime ( '-1 hour' , strtotime ( $date ) ) ;
            $date2 = date ( 'Y-m-d H:i:s' , $date2 );
            break;
        case "30M":
            $date2 = strtotime ( '-30 minute' , strtotime ( $date ) ) ;
            $date2 = date ( 'Y-m-d H:i:s' , $date2 );
            break;
        case "15M":
            $date2 = strtotime ( '-15 minute' , strtotime ( $date ) ) ;
            $date2 = date ( 'Y-m-d H:i:s' , $date2 );
            break;
        case "10M":
            $date2 = strtotime ( '-10 minute' , strtotime ( $date ) ) ;
            $date2 = date ( 'Y-m-d H:i:s' , $date2 );
            break;
        case "5M":
            $date2 = strtotime ( '-5 minute' , strtotime ( $date ) ) ;
        $date2 = date ( 'Y-m-d H:i:s' , $date2 );
            break;

        case "":
            exit();
    }
    

    $qry = "SELECT * FROM `summaries` WHERE MarketName = '$MarketName' AND date <= '$date2' ORDER BY id DESC LIMIT 1";
    $run = mysqli_query($conexion, $qry) or die(mysqli_error($conexion));
    $result = mysqli_fetch_assoc($run);
    
    $precio_Armame = number_format($result['Last'], 8, '.', ' ');
    $volumen_Armame = $result['Volumen'];
    
    $thread_id = mysqli_thread_id($conexion);

    mysqli_kill($conexion, $thread_id);
    
    mysqli_free_result($run);
    
    mysqli_close($conexion);
    
    return array($precio_Armame, $volumen_Armame);

}

I call this function from another file: (index3.php)

 

This "while" can be more than 200 rows...

.
.
.

$qry = "SELECT `MarketName` FROM `summaries` WHERE `MarketName` LIKE 'BTC%' GROUP BY MarketName HAVING COUNT(*) > 1";
    $run = mysqli_query($conexion, $qry) or die("Error: ". mysqli_error($conexion));
    $result2 = mysqli_fetch_assoc($run);
    mysqli_close($conexion);
    
    $i=0;
    if($result2){
        while ($result = mysqli_fetch_assoc($run)){

#24H
###################################################################################################################################


list($precio24H, $volumen24H) = armame($date, $result['MarketName'], '24H');

.
.
.

I always receive the message:

 

Warning: mysqli_connect(): (HY000/1203): User jpsoft_desa already has more than 'max_user_connections' active connections in /home/jpsoft/public_html/php/funciones.php on line 25
Error de conexion2: User jpsoft_desa already has more than 'max_user_connections' active connections.

 

 

 

But, look, in my function I always close the connections, kills the thread and free the result....

 

 

What It's wrong?

 

Posted

Well, my main recommendation starts with the fact that SQL is slow. PHP is fast. The way I do these things is I just pretty much dump the whole database into an array in PHP and then process it. Doing SQL queries in a while loop with 200 iterations is going to be painfully slow, and it's no wonder you get a max connection error. It would be even worse if you were using one of the new fangled asynchronous languages instead an old fashioned procedural language like PHP. Doing 201 SQL queries right in a row is going to take ages. Try doing one query outside the loop and then process the data in the loop. I bet you could cut the execution time down to like 10% of what it is now with this strategy.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...