how do i echo all rows in php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mkx
    Confirmed User
    • Nov 2003
    • 4001

    #1

    how do i echo all rows in php?

    Is there a way to show all rows in a database table without having to echo row[0] echo row [1] for each field?



    did select * from blah where id=1 but theres about 50 fields (keyword tags) so can i just echo them all?
  • SmellyNose
    Confirmed User
    • Aug 2009
    • 206

    #2
    It depends what you're using to get your data.

    You could do
    Code:
    foreach($data as $v) {
        echo $v;
    }
    Or if you're using mysql_fetch_array (or _assoc) you could do:

    Code:
    $r = mysql_fetch_assoc($mysql_resource_link_doo_dar_thing);
    while($r) {
        echo $r['tag']; // Use $r[index_of_Tag] if you are using _array
        $r = mysql_fetch_array($mysql_resource_link_doo_dar_thing);
    }
    I'm on ICQ if you need 594086663
    Last edited by SmellyNose; 01-14-2010, 09:59 PM.
    I'm a PHP developer - 594086663 - [email protected]

    Comment

    • mkx
      Confirmed User
      • Nov 2003
      • 4001

      #3
      thx dude, ill give it a try

      Comment

      • mkx
        Confirmed User
        • Nov 2003
        • 4001

        #4
        hmm cant figure it out, heres my code:

        Code:
        $alltags = mysql_query("SELECT * FROM tags WHERE user_id=$id");
        
        
        while($row1 = mysql_fetch_array($alltags))
          {
        echo $row1['tag_cen']; //i want to echo fields here instead of just tag_cen
        
          }

        Comment

        • Brujah
          Beer Money Baron
          • Jan 2001
          • 22157

          #5
          Code:
          while($row = mysql_fetch_array($alltags)) 
          {
             echo implode(', ',$row);
          }

          Comment

          • SmellyNose
            Confirmed User
            • Aug 2009
            • 206

            #6
            Originally posted by mkx
            hmm cant figure it out, heres my code:

            Code:
            $alltags = mysql_query("SELECT * FROM tags WHERE user_id=$id");
            
            
            while($row1 = mysql_fetch_array($alltags))
              {
            echo $row1['tag_cen']; //i want to echo fields here instead of just tag_cen
            
              }
            Add another echo in your while loop to see if it is actually going into it.

            And add:
            Code:
            if($alltags === false) {
                die("Query failed");
            }
            uk3.php.net/mysql_fetch_array

            And $id is definitely numeric isn't it?

            Code:
            $id = preg_replace('/[^0-9]/', '', $id);
            Otherwise you could be open to mysql injection
            I'm a PHP developer - 594086663 - [email protected]

            Comment

            • mkx
              Confirmed User
              • Nov 2003
              • 4001

              #7
              that works good, thanks very much guys,

              btw, is there anyway to skip the first field from echoing? since the first field is the id and i dont want to echo it. the 30 fields that follow are tags

              Comment

              • SmellyNose
                Confirmed User
                • Aug 2009
                • 206

                #8
                You could do it in MySQL, but I'd probably do it in the PHP as MySQL limits are usually what break things.

                You could use an int to see where you are, like this:
                Code:
                $c=0;
                while($row) {
                if($c==0) { break;}
                echo $row['tag'];
                $c++;
                }
                I'm a PHP developer - 594086663 - [email protected]

                Comment

                • mkx
                  Confirmed User
                  • Nov 2003
                  • 4001

                  #9
                  thanks again

                  Comment

                  • SmellyNose
                    Confirmed User
                    • Aug 2009
                    • 206

                    #10
                    No problem. Anybody looking for a web developer, ICQ Me: 594086663
                    I'm a PHP developer - 594086663 - [email protected]

                    Comment

                    • Richard - Triplexcash
                      Confirmed User
                      • Aug 2003
                      • 915

                      #11
                      Originally posted by AdultStoriesNow
                      No problem. Anybody looking for a web developer, ICQ Me: 594086663
                      Can you drop me an email please or hit me on my icq...can't seem to locate you with my IM client on ICQ.

                      Thanks.
                      Richard
                      [email protected]
                      ICQ: 322574184

                      Comment

                      Working...