WordPress MySQL result resource is not valid

I have wp_places custom table and I am getting this when I am printing array:

[0] => stdClass Object
        (
            [home_location] => 24
        )

[1] => stdClass Object
        (
            [home_location] => 29
        )

Now I want to implode value like this way (24,29) but in my code I am getting this error:

Read More
<b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource

My Code

$getGroupType = $_POST['parent_category'];
    $result = $wpdb->get_results( "SELECT home_location FROM wp_places WHERE blood_group LIKE '".$getGroupType."%'" );


    $bgroup = Array();
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $bgroup[] =  implode(',',$row);
    }
    echo implode(',',$bgroup);

Any ideas or suggestions? Thanks.

Related posts

Leave a Reply

2 comments

  1. $wpdb->get_results() already do the fetching for you, you don’t need to call mysql_fetch_array

    Given what you want to do, your code should look like this :

    $getGroupType = $_POST['parent_category'];
    $result = $wpdb->get_results( "SELECT home_location FROM wp_places WHERE blood_group LIKE '".$getGroupType."%'" );
    
    
    $bgroup = Array();
    foreach ($result as $location) {
        $bgroup[] =  $location->home_location;
    }
    echo '('.implode(',',$bgroup).')';
    
  2. It’s an PHP object that contains results, it isn’t a MySQL Result.

    Looking at the docs, it should be used like

    foreach ($result as $row) {
        $bgroup[] = $row->home_location;
    }
    echo implode(',',$bgroup)