www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Mysql multiple rows and separate using php

  1. #1
    Join Date
    Apr 2008
    Posts
    18

    resolved [RESOLVED] Mysql multiple rows and separate using php

    I am trying to find a way to pull multiple rows from a database and than be able to separate the rows and use them in php.

    PHP Code:
          $result mysql_query("SELECT url, image, placement FROM advert
          WHERE user='1'"
    ) OR die(mysql_error());
          
    //This grabs 3 rows with placement name equal to 'sideadtop','sideadmiddle','sideadbottom'

          
    ($row mysql_fetch_array($resultMYSQL_NUM));

          
    $keytop array_search('sideadtop'$row);
          
    $sideadtop['url'] == $row[$keytop]['url'];
          
    $sideadtop['image'] == $row[$keytop]['image'];

          
    $keymiddle array_search('sideadmiddle'$row);
          
    $sideadmiddle['url'] == $row[$keymiddle]['url'];
          
    $sideadmiddle['image'] == $row[$keymiddle]['image']; 
    I am trying to get the url and image values for each ad placement value. Should I be using mysql to pull individual rows or use php to separate them if possible. Or is there a better way to do this?

    Any help is appreciated

    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,622
    Not sure if this is what you are asking, but typically you create your SQL to retrieve all the desired rows, then use a while() loop to process each result row:
    PHP Code:
    $sql "SELECT blah blah blah...";
    $result mysql_query($sql);
    while(
    $row mysql_fetch_assoc($result)) {
       
    // do stuff with $row to process/output this result row

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2008
    Posts
    18
    hey thanks for the reply...

    So if I did it with a while loop, would it be possible to get a specific variable.

    For example say i want to get

    $row['0']['url']
    $row['1']['url']
    $row['2']['url']

    and use them within another function... I need to get speficic "url" field based on the placement field.

    So if placement == "sideadtop" I would like to get the url that is in the row of that placement.

    I would like to know if I have to do this with individual calls to MYSQL getting one row ata time or if there is a way to get all 3 rows and seperate them as needed?

    Thanks again

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,622
    You can easily dump the results into an array (or object) for later use, e.g.:
    PHP Code:
    $data = array();
    while(
    $row mysql_fetch_assoc($result)) {
       
    $data[] = $row;
    }
    print_r($data); 
    Or you could use one of the query column keys as the first array dimension key:
    PHP Code:
    $data = array();
    while(
    $row mysql_fetch_assoc($result)) {
       
    $data[$row['id']] = $row;
    }
    print_r($data); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Apr 2008
    Posts
    18
    Hey there,

    thanks so much I never knew or thought you could use the column id.
    PHP Code:
    $data[$row['id']] = $row
    That worked awsome, i really appreciate it - cheers

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles