0

Hello I'm currently trying to create a page based on a database under mysql that would update itself for a client. However what I'm trying to do loops and returns the first value of the database each time and indefinetely when I want it to go on to another object in the database. Here is the code, I'm a beginner so the error might be flagrant, thanks for the help.

<?php
    
try
    {

        $db = new PDO('mysql:host=localhost;dbname=labase', 'root' ,'');
        $db->exec(" SET CHARACTER SET utf8 ");
        $db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(Exception $e){
            
        echo'une erreur est survenue';
        die();
            
    }

    

for ($i = 1; $i < 10; $i++) {
    if ($i=1){
        $select = $db->prepare("Select profession from contact where affiliation='nord' group by profession"); // je récupère les professions de la bdd
        $select->execute();
    }
    $data = $select->fetch(PDO::FETCH_OBJ);
    $profess=$data->profession; // je prends la prochaine profession
    
    $selectionner = $db->prepare("Select nomcontact, count(*) as nbrcontact from contact where affiliation='nord' and profession='$profess'"); // je prends les contacts qui ont cette profession ainsi que leur nombre
    $selectionner->execute();
    
    $prendre = $selectionner->fetch(PDO::FETCH_OBJ);
    $nbrcontact=$prendre->nbrcontact;// je récupère leur nombre
echo $profess;
echo $nbrcontact;

}
    
    ?>
gp_sflover
  • 3,460
  • 5
  • 38
  • 48
qYUUU
  • 33
  • 1
  • 5

3 Answers3

0

I am not a PHP expert and never use PDO, but in Msqli, there is a fetch_array() to get multiple result (instead of fetch for single result), maybe in PDO you have a fetch_array too. Then, you can loop on the result array

Something like that (using msqli)

$sql = "SELECT... FROM ..";

        $result = $link->query($sql);

        while($row =mysqli_fetch_array($result))

{ }

Maxime V
  • 31
  • 8
0

if ($i=1) { // here is should be == or ===

Roman Samarsky
  • 320
  • 3
  • 10
0

You're causing an infinite loop by declaring $i=1

<?php
    
try
    {

        $db = new PDO('mysql:host=localhost;dbname=labase', 'root' ,'');
        $db->exec(" SET CHARACTER SET utf8 ");
        $db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(Exception $e){
            
        echo'une erreur est survenue';
        die();
            
    }

    

for ($i = 1; $i < 10; $i++) {
    if ($i == 1){ // added code
        $select = $db->prepare("Select profession from contact where affiliation='nord' group by profession"); // je récupère les professions de la bdd
        $select->execute();
    }
    $data = $select->fetch(PDO::FETCH_OBJ);
    $profess=$data->profession; // je prends la prochaine profession
    
    $selectionner = $db->prepare("Select nomcontact, count(*) as nbrcontact from contact where affiliation='nord' and profession='$profess'"); // je prends les contacts qui ont cette profession ainsi que leur nombre
    $selectionner->execute();
    
    $prendre = $selectionner->fetch(PDO::FETCH_OBJ);
    $nbrcontact=$prendre->nbrcontact;// je récupère leur nombre
echo $profess;
echo $nbrcontact;

}
    
    ?>

Use == for comparison

Dominic F
  • 9
  • 1
  • 6