Professional Web Applications Themes

[PHP] problem looping through records - PHP Development

Huzz wrote: > I have a sql statement which brings about 15 records... inside the while > loop i am trying to get data from another table by passing an id, the > problem is i am only getting the first record .. not the whole 15 records.. > > any suggestion?? > > function list_ss_cats(){ > global $db; > $sql = "SELECT DISTINCT(new_cat) FROM logos WHERE format =3"; > $result = $db->sql_query($sql); > $num = $db->sql_numrows(); > while($row = $db->sql_fetchrow()){ > > $sql2 = "SELECT * FROM cats WHERE id='$row[new_cat]'"; > > $result1 = $db->sql_query($sql2); > > $row1 = ...

  1. #1

    Default Re: [PHP] problem looping through records

    Huzz wrote:
    > I have a sql statement which brings about 15 records... inside the while
    > loop i am trying to get data from another table by passing an id, the
    > problem is i am only getting the first record .. not the whole 15 records..
    >
    > any suggestion??
    >
    > function list_ss_cats(){
    > global $db;
    > $sql = "SELECT DISTINCT(new_cat) FROM logos WHERE format =3";
    > $result = $db->sql_query($sql);
    > $num = $db->sql_numrows();
    > while($row = $db->sql_fetchrow()){
    >
    > $sql2 = "SELECT * FROM cats WHERE id='$row[new_cat]'";
    >
    > $result1 = $db->sql_query($sql2);
    >
    > $row1 = $db->sql_fetchrow($result1);
    > echo $row1[en]; // this should show all 15 records but only shows the first
    > record
    Yeah, that's all it should show. It's going to show you the value for
    the 'en' column. Try a print_r($row1) so you can see what you've got.

    --
    ---John Holmes...

    Amazon Wishlist: [url]www.amazon.com/o/registry/3BEXC84AB3A5E/[/url]

    PHP|Architect: A magazine for PHP Professionals [url]www.phparch.com[/url]




    John W. Holmes Guest

  2. #2

    Default Re: [PHP] problem looping through records

    On Sun, 10 Aug 2003 22:11:09 +0100, you wrote:
    >I have a sql statement which brings about 15 records... inside the while
    >loop i am trying to get data from another table by passing an id, the
    >problem is i am only getting the first record .. not the whole 15 records..
    >
    >any suggestion??
    >
    >function list_ss_cats(){
    >global $db;
    >$sql = "SELECT DISTINCT(new_cat) FROM logos WHERE format =3";
    >$result = $db->sql_query($sql);
    >$num = $db->sql_numrows();
    >while($row = $db->sql_fetchrow()){
    >
    > $sql2 = "SELECT * FROM cats WHERE id='$row[new_cat]'";
    Others have answered your immediate question, but it looks to me like you
    should be doing a join and only using one SQL statement instead of sixteen.
    Something along the lines of

    SELECT DISTINCT(logos.new_cat), cats.*
    FROM logos, cats
    WHERE logos.format = 3
    AND logos.new_cat = cats.id

    (Though I'd remove the * personally, it can slow things down. Oh, and you
    get $num but never actually use it, and "$row1[en]" should be throwing a
    warning - it should be "$row1['en']").

    David Otton Guest

Similar Threads

  1. Problem Looping
    By mushBug in forum Coldfusion - Getting Started
    Replies: 6
    Last Post: November 5th, 07:44 PM
  2. problem looping through queries
    By TimMcGeary in forum Coldfusion Database Access
    Replies: 9
    Last Post: July 23rd, 07:19 AM
  3. Problem with looping over cfhttp
    By cjdunkerley in forum Coldfusion - Advanced Techniques
    Replies: 0
    Last Post: June 21st, 11:47 AM
  4. looping over a list problem
    By jmercmon in forum Coldfusion - Advanced Techniques
    Replies: 4
    Last Post: April 8th, 10:52 PM
  5. Looping Music - Problem!!
    By ventureforth webforumsuser@macromedia.com in forum Macromedia Director Basics
    Replies: 2
    Last Post: June 30th, 06:55 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139