Need help with database array

Discussion in 'PHP' started by resting, Jul 2, 2009.

  1. #1
    Hi guys,

    I used a while loop to put all mysql result sets into $all_products array.

    After that I used a foreach loop to grab all those dump for mysql_fetch_assoc to generate a table.

    I'm able to get the returns for print_r above as:
    however, PHP produces the following: PHP Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

    Question:Is it because I stored the mysql dump into an array instead of using mysql_fetch_assoc to get the data, right after the execution of query thats causing the problem?
     
    resting, Jul 2, 2009 IP
  2. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you might have an error in your query!
     
    zeronese, Jul 2, 2009 IP
  3. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah you're doing that a really funny way. You're making an array of an array and foreach'ing the print_r. Also the error means you have a problem with your query.

    Try something like this instead:

     
    Goramba, Jul 2, 2009 IP
  4. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #4
    I agree with Goramba. You are doing it in a funny way. But you must have your reasons for doing it that way.
    About this error:
    You are getting thie error becuase you are passing '$all_products_set' to the mysql_fetch_assoc function.

    But becuase you did this on your first 'while' loop:
    while (something){
    $temp_set = mysql_query($query);
    $all_products[] = Array("products" => $temp_set)
    } 
    PHP:
    '$all_products_set' has been set as array and NOT the resource id.

    if you change
    $all_products[] = Array("products" => $temp_set)
    PHP:
    to
    $all_products[] = $temp_set;
    PHP:
    , it should be work fine.
     
    samyak, Jul 2, 2009 IP
  5. resting

    resting Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    this looks cool. going to try it when i get back to office. :)

    well i'm doing it because i'm get a whole list of records from 5 tables and displaying them in 1 table. any tables that are not empty gets put inside the $all_products array. only then will another while loop retrieve the data from those resource indexes.

    maybe there's a easier method by using WHERE condition though...
     
    resting, Jul 2, 2009 IP
  6. resting

    resting Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    great! that statement works just fine.
    problem here was i converted the resource index into arrays with my mediocre understanding of arrays :( haa..

    learned something again. cool. thanks.

    oh btw, i'm experimenting with the select union command to combine the 5 table's results instead of using php to combine them.
     
    resting, Jul 2, 2009 IP