SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jan 2012
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DB Query (Can't display multiple rows)

    I have a database query that displays pages for various animal species (e.g. MySite/Animals/Tiger). My code looks like this. (I deleted some of the field names to make it a little more readble.)

    Code:
    $Life = mysql_fetch_assoc(mysql_query("SELECT L.Taxon, G.TaxonG, I.MyURL
     FROM gz_life L
     LEFT JOIN gz_ghosts as G ON G.TaxonG = L.Taxon
     WHERE L.Taxon LIKE '$MyURL'"));
    
    $MyName = str_replace('_', ' ', $Life['Taxon']);
    $Parent = $Life['Parent'];
    $MyFam = $Life['MyFam'];
    $MyOrd = $Life['MyOrd'];
    $MyCla = $Life['MyCla'];
    $Rank = $Life['Rank'];
    $Ghost = $Life['Wild_Card'];
    $Wild_Card = $Ghost;
    I created another database table (gz_images) to help me display images. So I changed my query to this:

    Code:
    $Life = mysql_fetch_assoc(mysql_query("SELECT L.Taxon, G.TaxonG, I.MyURL
     FROM gz_life L
     LEFT JOIN gz_ghosts as G ON G.TaxonG = L.Taxon
     LEFT JOIN gz_images as I ON I.MyURL = L.Taxon
     WHERE L.Taxon LIKE '$MyURL'"));
    ...and I added some more echo values:

    Code:
    $Img = $Life['Img'];
    $ImgName = $Life['ImgName'];
    $Width = $Life['Width'];
    $Height = $Life['Height'];
    $Div = $Life['Div'];
    $Source = $Life['Source'];
    $Permission = $Life['Permission'];
    $Caption = $Life['Caption'];
    It works to a point; it only displays one image.

    Here's some more code...

    Code:
    switch($Img)
    {
     case 'Intro1':
     $ImgIntro1 = '(Whatever)';
     break;
     case 'Intro2':
     $ImgIntro2 = '(Whatever)';
     break;
     default:
     break;
    }
    I then echo $ImgIntro1 and $ImgIntro2 on my pages. However, only $ImgIntro1 displays an image.

    If I have TWO pictures of a wolf, my DB table would look something like this...

    Code:
    MyURL  Img
    Wolf   Intro1
    Wolf   Intro2
    In other words, there are multiple Wolf rows (field MyURL), but my code is apparently just picking up the first row.

    I changed the query from a left join to a right join, but that didn't work. Then I inserted a WHILE LOOP, as follows...

    Code:
    // IMAGES...
    // while ($row = mysql_fetch_array($Life, MYSQL_ASSOC))
    while ($row = mysql_fetch_assoc($Life))
    {
    $Img = $Life['Img'];
    $ImgName = $Life['ImgName'];
    $Width = $Life['Width'];
    $Height = $Life['Height'];
    $Div = $Life['Div'];
    $Source = $Life['Source'];
    $Permission = $Life['Permission'];
    $Caption = $Life['Caption'];
    }
    However, I'm not very familiar with while loops. I tried several variations, but nothing works.

    Can anyone tell me what the solution is?

    Thanks.

  2. #2
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    	$sql = 'SQL QUERY';
    	$result = mysql_query($sql);
    	while( $row = mysql_fetch_assoc($result) ) {
    		echo $row['img'];
    	}
    It is shown the manual:
    http://us2.php.net/manual/en/functio...etch-array.php
    http://us2.php.net/manual/en/functio...etch-assoc.php

  3. #3
    SitePoint Addict
    Join Date
    Jan 2012
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That works great. Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •