www.webdeveloper.com
Results 1 to 6 of 6

Thread: How do I turn a resource into an array?

  1. #1
    Join Date
    Mar 2010
    Posts
    701

    How do I turn a resource into an array?

    I want to transform the output of mysql_query("SELECT...) into an array. How can that be done?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,326
    You have to use one of the mysql_fetch_*() functions in a loop.
    PHP Code:
    $result mysql_query($sql);
    $data = array();
    while(
    $row mysql_fetch_assoc($result))
    {
       
    $data[] = $row;
    }
    // view array:
    echo "<pre>".print_r($datatrue)."</pre>"
    "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
    Mar 2010
    Posts
    701
    I got
    Code:
    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in...

  4. #4
    Join Date
    Dec 2009
    Location
    Denmark
    Posts
    240
    You might want to make sure that your query does not return false.

    PHP Code:
    if(($result mysql_query($sql)) !== false) {
        while(
    $row mysql_fetch_assoc($result)) {
           
    $data[] = $row;
        } 

    }else {
        echo 
    mysql_error()


  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,326
    That means your query was rejected by MySQL for some reason, and thus returned boolean false instead of a query result resource. Add an if() condition to see if $result (or whatever variable you used for the mysql_query() return value) is false, and if so log or display the value of mysql_error() to find out what it didn't like. You might also want to include the query string in that debug info.
    PHP Code:
    $result mysql_query($sql);
    if(
    $result == false)
    {
       
    user_error(mysql_error()."<br />\n$sql");
       echo 
    "<p class='error'>Oops! Database error.</p>";
    }
    else
    {
       
    // do your fetch and other processing with the query result....

    "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

  6. #6
    Join Date
    Mar 2010
    Posts
    701
    I finally found the error, I added a "s" after $result for one of them. Carelessness

Thread Information

Users Browsing this Thread

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

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