Jump to content

problem in "while($row=mysqli_fetch_assoc($result)) "


cty

Recommended Posts

 

<?php 
   session_start(); 

         echo'Welcome<font size=5> '; 
         echo $_SESSION['valid_user']; 
         echo'</font><br />'; 
         echo'<p>'; 
         echo'This is My Account page'; 

         $db=new mysqli('localhost','root',''); 

         if (mysqli_connect_errno()) { 
              die( 'Connect failed: ' . mysqli_connect_error() ); 
         } 
         $db->select_db('test') 
         or die( 'select_db failed: '.$db->error ); 

         $x=$_SESSION['valid_user']; 




         $query="select userid from user where username='$x'"; 
         $result=$db->query($query) 
         or die('query failed'.$db->error); 

         $row=$result->fetch_assoc(); 



         $y=$row['userid']; 




         $query='SELECT * FROM buybook,book'." WHERE userid='$y'". 
         "and book.bookid=buybook.bookid"; 

         $result=$db->query($query) 
         or die( 'query failed: '.$db->error ); 
          
       
         echo"<p><font color=blue> Book I Buy</font></p>"; 

         if(mysqli_num_rows($result)>0) 
         { 
         echo"<table border=1 align=center width=80% bgcolor=#99CCFF>"; 

         echo "<tr>"; 

         echo"<td align=center> ISBN </td>"; 
         echo "<td align=center> TITLE </td>"; 
         echo "<td align=center> PRICE </td>"; 
         echo"<td align=center> CONDITION</td>"; 
         echo "<td align=center> DATE</td>"; 
         echo "<td align=center> TIME</td>"; 
          
         echo "<td align=center>SELLER</td>";//test 

         echo"</tr>"; 

         while($row=mysqli_fetch_assoc($result)) 
         { 
    
         echo"<tr>"; 

         echo"<td align=center>".$row['isbn']."</td>"; 
         echo"<td align=center>".$row['title']."</td>"; 
         echo"<td align=center>RM&nbsp".$row['price']."</td>"; 
         echo"<td align=center>".$row['condition']."</td>"; 

         echo"<td align=center>".$row['buydate']."</td>"; 
         echo"<td align=center>".$row['buytime']."</td>"; 
    
         $k=$row['isbn']; 
         $query='SELECT userid FROM sellbook,book'." WHERE isbn='$k'". 
         "and book.bookid=sellbook.bookid"; 
          
         $result=$db->query($query) 
         or die('query failed'.$db->error); 

         $row=$result->fetch_assoc(); 


         $s=$row['userid']; 

         $query='SELECT username FROM user'." WHERE userid='$s'"; 
          
         $result=$db->query($query) 
         or die('query failed'.$db->error); 

         $row=$result->fetch_assoc(); 


         echo"<td align=center>".$row['username']."</td>"; 
         echo"</tr>"; 
         } 
          
          
         echo"</table>"; 
         } 
         else 
         { 
         echo"No data found!"; 
         } 



?>

---------------------------------------

Problem:

 

The  $row['username'] unable to display when

$row=mysqli_fetch_assoc($result) are more than one.

 

For example,

If the result is only one,then the $row['username'] able to display,

BUT, when the result is more than one,$row['username'] cannot be show.

 

In addtion, $row['isbn']$row['title']$row['price']

$row['condition']$row['buydate']$row['buytime']

only display once,

meaning,if search result have 3 record,only one record will be shown ,

and the username wil totally gone!

 

Can any one able to edit for me?

 

   

Your problem lies here

 

         $k=$row['isbn']; 
         $query='SELECT userid FROM sellbook,book'." WHERE isbn='$k'". 
         "and book.bookid=sellbook.bookid"; 
          
         $result=$db->query($query) 
         or die('query failed'.$db->error); 

         $row=$result->fetch_assoc(); 


         $s=$row['userid']; 

         $query='SELECT username FROM user'." WHERE userid='$s'"; 
          
         $result=$db->query($query) 
         or die('query failed'.$db->error); 

         $row=$result->fetch_assoc(); 


         echo"<td align=center>".$row['username']."</td>"; 
         echo"</tr>"; 
         } 

 

You are reassigning $result, which is what is powering the while. Rename that to another name IE: $result2 and see what happens.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.