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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cty
Forum Newbie
Posts: 4
Joined: Thu Dec 14, 2006 11:03 pm

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

Post by cty »

Code: Select all

<?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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your code overwrites $result in the loop.
Post Reply