Jump to content

[SOLVED] Display one mysql_row


fighnight

Recommended Posts

I am having this trouble, I am trying to make an list of how many stuff someone has but I am having this error it shows two rows

 

Like

 

Item Here                Amount

 

Baseball Bat              2

 

Baseball Bat              2

 

 

I am trying to make it only display one but I can't figure it out. :[

Link to comment
https://forums.phpfreaks.com/topic/85321-solved-display-one-mysql_row/
Share on other sites

 

<tr><td> Item </td> <td> Amount </td> </tr>
$item_info= mysql_query("SELECT * FROM item WHERE owner='1'");

while($iteminfo= mysql_fetch_array($item_info)
{
$result1 = mysql_query("SELECT * FROM item WHERE item = '$iteminfo[item]'");

$idu=mysql_num_rows($result1);
echo "<tr><td>$iteminfo[item]</td><td>$idu</td></tr>";
}

 

Very Weird but ehh....

 

Are you just trying to display how many items are in the item table under that owner?

 

<?
echo '<tr><td>Item Amount</td></tr>
$query = ("SELECT * FROM item WHERE owner='1'");
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
echo $num_rows;
?>

 

Or, is there only one item under the owners name, but it's displaying it twice? Or are there two items, but you only want the first displayed?

the while is a loop, it picks everything out that: WHERE owner='1' it means it will keep going no matter what the person.  I.e. it will go for a user name fred that is an owner if they own 2 items it will pull that one item once then pull the next item for that user.

 

your statement is only for pulling one item at a time no matter the user.

Try this:

 

<tr><td>Item Amount</td></tr>
<?
$query = ("SELECT * FROM item WHERE owner='1'");
$result = mysql_query($query);
for($count=0; $count<1; ++$count)  {
while($iteminfo = mysql_fetch_assoc($result)) {

echo "<tr><td>$iteminfo[item]</td><td>$idu</td></tr>";
}}
?>

your code should be I've simplified some things

 

<tr><td> Item </td> <td> Amount </td> </tr>
$item_info= mysql_query("SELECT count(*) as count,item FROM item WHERE owner='1' group by item");

while($iteminfo= mysql_fetch_array($item_info)
{
echo "<tr><td>{$iteminfo['item']}</td><td>{$iteminfo['count']}</td></tr>";
}

 

tell me if it works!!!

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.