Jump to content


Photo

returning stuff from database confusing


  • Please log in to reply
5 replies to this topic

#1 adamhhh

adamhhh
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 04 September 2006 - 03:48 PM

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'.

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

}
?>

any ideas?

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

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 03:52 PM

You need to add this:
$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;

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

This should do:
<?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 />";
}

?>


#3 adamhhh

adamhhh
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 04 September 2006 - 04:03 PM

$rp??

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 04 September 2006 - 04:05 PM

$rp??


It's just the name of an array. It could have been almost anything and wouldn't affect the code.
Legend has it that reading the manual never killed anyone.
My site

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 04:11 PM

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.

#6 adamhhh

adamhhh
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 04 September 2006 - 04:12 PM

never mind it all works now :) bit of playing about with braces :P




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users