Jump to content

Archived

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

adamhhh

returning stuff from database confusing

Recommended Posts

hi guys, im having a few problems returning stuff from a database and this is a problem ive had before and not been able to solve. Basically i retrieve lots of stuff, however it only displays the last item that has been added rather than displaying everything. how do i get it to display everything?? i just cant figure it out!!

This is a simple page to show what im trying to achieve, its just returning information regarding products people have added to a mock 'wishlist'.

[code]<?php

require 'incs/php.php';
$conx = @mysql_connect("$dbServer", "$dbUser", "$dbPass");
@mysql_select_db("$dbName");
if (!$conx)
{
echo ("No Database Access<br />Please try again");
exit;
}


$result = mysql_query("select * FROM wishlist where login='test'");

while($r=mysql_fetch_array($result))
{
    $id=$r["id"]; 
    $login=$r["login"];
}

$result2 = mysql_query("select * FROM product where id=$id");
while($r=mysql_fetch_array($result2))
{
    $prodId=$r["id"];
    $manuf=$r["manuf"];
    $name=$r["name"];
    $description=$r["description"];
    $size=$r["size"];
    $type=$r["type"];
    $fibre=$r["fibre"];
    $dimensions=$r["dimensions"];
    $image=$r["image"];

echo $prodId; 
echo "<br />";
echo $manuf;
echo "<br />";
echo $name;
echo "<br />";
echo $description;
echo "<br />";
echo $size;
echo "<br />";
echo $type;
echo "<br />";
echo $fibre;
echo "<br />";
echo $dimensions;

}
?>[/code]

any ideas?

[b]EDITED BY WILDTEEN88: PLEASE USE [nobbc][CODE][/CODE][/nobbc] OR THE [nobbc][PHP][/PHP][/nobbc] TAGS WHEN POSTING CODE.[/B]

Share this post


Link to post
Share on other sites
You need to add this:
[code]$result2 = mysql_query("select * FROM product where id=$id");
while($r=mysql_fetch_array($result2))
{
    $prodId=$r["id"];
    $manuf=$r["manuf"];
    $name=$r["name"];
    $description=$r["description"];
    $size=$r["size"];
    $type=$r["type"];
    $fibre=$r["fibre"];
    $dimensions=$r["dimensions"];
    $image=$r["image"];

echo $prodId; 
echo "<br />";
echo $manuf;
echo "<br />";
echo $name;
echo "<br />";
echo $description;
echo "<br />";
echo $size;
echo "<br />";
echo $type;
echo "<br />";
echo $fibre;
echo "<br />";
echo $dimensions;

}[/code] Into your first while loop, otherwise it'll just retrun the last result from the first query.

This should do:
[code]<?php

require 'incs/php.php';

$conx = @mysql_connect($dbServer, $dbUser, $dbPass);

@mysql_select_db($dbName) or die("No Database Access<br />Please try again");

$result = mysql_query("select * FROM wishlist where login='test'");

while($r = mysql_fetch_array($result))
{
    $id = $r["id"];
    $login = $r["login"];

    $result2 = mysql_query("select * FROM product where id=$id");

    $rp = mysql_fetch_array($result2))

    $prodId      = $rp["id"];
    $manuf      = $rp["manuf"];
    $name        = $rp["name"];
    $description = $rp["description"];
    $size        = $rp["size"];
    $type        = $rp["type"];
    $fibre      = $rp["fibre"];
    $dimensions  = $rp["dimensions"];
    $image      = $rp["image"];

    echo $prodId . "<br />\n";
    echo $manuf . "<br />\n";
    echo $name . "<br />\n";
    echo $description . "<br />\n";
    echo $size . "<br />\n";
    echo $type . "<br />\n";
    echo $fibre . "<br />\n";
    echo $dimensions;

    echo "<br /><br />";
}

?>[/code]

Share this post


Link to post
Share on other sites
[quote author=adamhhh link=topic=106853.msg427829#msg427829 date=1157385836]
$rp??
[/quote]

It's just the name of an array. It could have been almost anything and wouldn't affect the code.

Share this post


Link to post
Share on other sites
I used $rp because if I used $r for retriving the results from the secound query, it would of overwirte the first set of results, which will result in your script not working correctly.

Share this post


Link to post
Share on other sites

×

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.