/    Sign up×
Community /Pin to ProfileBookmark

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

[code=php]<?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!”;
}

?>[/code]

—————————————
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?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@bathurst_guyMar 23.2007 — You need to give each instance of your queries different variable names otherwise they overwrite each other. $result.
×

Success!

Help @cty spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...