Mysql_fetch_row没有用多个结果填充SESSION数组

时间:2013-09-06 15:43:43

标签: php mysql session-variables

Hello这是我的解决方案,因为我对SQL语句提出了一些建议......谢谢大家

$connection = ConnectionBD();
$tag = $_SESSION['nomUtilisateur'];
$panier= array();
$requete = mysql_query("SELECT * FROM cours INNER JOIN elevecours ON cours.idcours = elevecours.IDCours WHERE elevecours.IDEleve= '$tag'",$connection);
$H = 0;
        if(mysql_num_rows($requete) != 0)
        {
            while($row = mysql_fetch_row($requete))
            {
                //$panier[] = $row;
                $_SESSION['Panier']['Id'][$H]        = $row[0];
                $_SESSION['Panier']['CodeCours'][$H] = $row[1];
                $_SESSION['Panier']['Titre'][$H]     = $row[2];
                $_SESSION['Panier']['Prealable'][$H] = $row[3];
                $_SESSION['Panier']['NbHeure'][$H]   = $row[4];
                $_SESSION['Panier']['Session'][$H]   = $row[5];
                $_SESSION['Panier']['Credit'][$H]    = $row[6];
                $H ++;
            }
        }
        mysql_close($connection);

3 个答案:

答案 0 :(得分:0)

循环中每次迭代需要mysql_fetch_row一次。

for($i = 0; $i < mysql_num_rows($req); $i++) {
     $resultat = mysql_fetch_row($req);
     //the rest of your code goes here
}

答案 1 :(得分:0)

在你的第二个foreach循环中设置$ H = 0;然后$ H ++在同一个循环中,这将不会正确递增,因为下次它再次越过循环$ H = 0;语句将导致此值为0,因此将继续覆盖您的数据,直到循环完成并且您最后只剩下一个$ _SESSION值。

                for($j = 0; $j < count($panier); $j++)
                { 

                        if($resultat[$i] == $panier[$j][0])
                        {
                            $H = 0; <-- sets to zero on every interation 
                            $_SESSION['Panier']['Id'][$H] = $result[0];
                            $_SESSION['Panier']['CodeCours'][$H] = $result[1];
                            $_SESSION['Panier']['Titre'][$H] = $result[2];
                            $_SESSION['Panier']['Prealable'][$H] = $result[3];
                            $_SESSION['Panier']['NbHeure'][$H] = $result[4];
                            $_SESSION['Panier']['Session'][$H] = $result[5];
                            $_SESSION['Panier']['Credit'][$H] = $result[6];
                            $H++;
                        }
                }

如果打算迭代$ H变量,建议你将它移到循环之外。

答案 2 :(得分:0)

这些是我的测试变量...

   $session = array(); 
   $panier = array(array(1,'one'),array(2,'two'),array(3,'three'),);

   $req = array(
         array(1,'one','Titre1','Prealable1','NbHeure1','Session1','Credit1'),
         array(2,'two','Titre2','Prealable2','NbHeure2','Session2','Credit2'),
         array(3,'three','Titre3','Prealable3','NbHeure3','Session3','Credit3'),
         array(4,'four','Titre4','Prealable4','NbHeure4','Session4','Credit4'),
         array(5,'five','Titre5','Prealable5','NbHeure5','Session5','Credit5'),);

我编写的代码完全不依赖于您的数据库和数据,因为我不知道它是什么......不知道。切换出一些代码行来使用你的数据库。

   // $req = mysql_query("SELECT * FROM cours", $connection);  uncomment this line for your code

   if ($req!==false) 
   {
      foreach( $req as $i => $resultat )             // testing row, remove for real
      // while( $resultat = mysql_fetch_row($req) )  // real row, uncomment for you 
      {
         $compare = $resultat[0];  
         // we will be using $compare to see if this row
         // matches one in $panier.. this needs to be changed
         // appropriately.  I don't have enough info to define it....
         // this needs to be a unique field, or it should be 
         // it is going to be used like this:  if($resultat[$i] == $panier[$j][0])

         $result[ $compare ] = $resultat;
      } 

      $hi = 0;  
      foreach( $panier as $index => $panier_row )
      {                        
         $compare = $panier_row[0]; 

         if (isset($result[$compare]))
         {
            $H = 0;

            // change $session to $_SESSION for your code...  
            $session['Panier']['Id'][$hi][$H]        = $result[$compare][0];
            $session['Panier']['CodeCours'][$hi][$H] = $result[$compare][1];
            $session['Panier']['Titre'][$hi][$H]     = $result[$compare][2];
            $session['Panier']['Prealable'][$hi][$H] = $result[$compare][3];
            $session['Panier']['NbHeure'][$hi][$H]   = $result[$compare][4];
            $session['Panier']['Session'][$hi][$H]   = $result[$compare][5];
            $session['Panier']['Credit'][$hi][$H]    = $result[$compare][6];
            $H++;
            $hi++;  
         }
      }        
   }   

我的结果显示$session['Panier']['Credit']有1,2和3但不是4或5的三样东西,因为它们不匹配$panier

所有内容$session中的内容:

array (
  'Panier' => 
  array (
    'Id' => 
    array (
      0 => 
      array (
        0 => 1,
      ),
      1 => 
      array (
        0 => 2,
      ),
      2 => 
      array (
        0 => 3,
      ),
    ),
    'CodeCours' => 
    array (
      0 => 
      array (
        0 => 'one',
      ),
      1 => 
      array (
        0 => 'two',
      ),
      2 => 
      array (
        0 => 'three',
      ),
    ),
    'Titre' => 
    array (
      0 => 
      array (
        0 => 'Titre1',
      ),
      1 => 
      array (
        0 => 'Titre2',
      ),
      2 => 
      array (
        0 => 'Titre3',
      ),
    ),
    'Prealable' => 
    array (
      0 => 
      array (
        0 => 'Prealable1',
      ),
      1 => 
      array (
        0 => 'Prealable2',
      ),
      2 => 
      array (
        0 => 'Prealable3',
      ),
    ),
    'NbHeure' => 
    array (
      0 => 
      array (
        0 => 'NbHeure1',
      ),
      1 => 
      array (
        0 => 'NbHeure2',
      ),
      2 => 
      array (
        0 => 'NbHeure3',
      ),
    ),
    'Session' => 
    array (
      0 => 
      array (
        0 => 'Session1',
      ),
      1 => 
      array (
        0 => 'Session2',
      ),
      2 => 
      array (
        0 => 'Session3',
      ),
    ),
    'Credit' => 
    array (
      0 => 
      array (
        0 => 'Credit1',
      ),
      1 => 
      array (
        0 => 'Credit2',
      ),
      2 => 
      array (
        0 => 'Credit3',
      ),
    ),
  ),
)

就个人而言......你应该使用mysql_fetch_assoc并按照这样做:

   $session = array(); 
   $panier = array(array(1,'one'),array(2,'two'),array(3,'three'),);

   $req = array(
        array('id'=>1,'var'=>'one','Titre'=>'Titre1','Prealable'=>'Prealable1','NbHeure'=>'NbHeure1','Session'=>'Session1','Credit'=>'Credit1'), 
        array('id'=>2,'var'=>'two','Titre'=>'Titre2','Prealable'=>'Prealable2','NbHeure'=>'NbHeure2','Session'=>'Session2','Credit'=>'Credit2'), 
        array('id'=>3,'var'=>'thr','Titre'=>'Titre3','Prealable'=>'Prealable3','NbHeure'=>'NbHeure3','Session'=>'Session3','Credit'=>'Credit3'), 
        array('id'=>4,'var'=>'fou','Titre'=>'Titre4','Prealable'=>'Prealable4','NbHeure'=>'NbHeure4','Session'=>'Session4','Credit'=>'Credit4'), 
        array('id'=>5,'var'=>'fiv','Titre'=>'Titre5','Prealable'=>'Prealable5','NbHeure'=>'NbHeure5','Session'=>'Session5','Credit'=>'Credit5'),); 


   // $req = mysql_query("SELECT * FROM cours", $connection); 

   if ($req!==false) 
   {
      foreach( $req as $i => $resultat )             // testing row, remove for real
      // while( $resultat = mysql_fetch_assoc($req) )  // real row, uncomment for you 
                                                       //  mysql_fetch_assoc !!
      {
         $compare = $resultat['id'];  
         // we will be using $compare to see if this row
         // matches one in $panier.. this needs to be changed
         // appropriately.  I don't have enough info to define it....
         // this needs to be a unique field, or it should be 

         $result[ $compare ] = $resultat;
      } 

      foreach( $panier as $index => $panier_row )
      {                        
         $compare = $panier_row[0]; 

         if (isset($result[$compare]))
         { 
            $session[] = $result[$compare];  
         }
      }        
   }   

这是$session

array (
  0 => 
  array (
    'id' => 1,
    'var' => 'one',
    'Titre' => 'Titre1',
    'Prealable' => 'Prealable1',
    'NbHeure' => 'NbHeure1',
    'Session' => 'Session1',
    'Credit' => 'Credit1',
  ),
  1 => 
  array (
    'id' => 2,
    'var' => 'two',
    'Titre' => 'Titre2',
    'Prealable' => 'Prealable2',
    'NbHeure' => 'NbHeure2',
    'Session' => 'Session2',
    'Credit' => 'Credit2',
  ),
  2 => 
  array (
    'id' => 3,
    'var' => 'thr',
    'Titre' => 'Titre3',
    'Prealable' => 'Prealable3',
    'NbHeure' => 'NbHeure3',
    'Session' => 'Session3',
    'Credit' => 'Credit3',
  ),
)