hello. could someone glance at this code and explain to me what I need to do to make it work? I'm trying to separate the results of my query into a comma-separated array. Thx.

$query_records = "SELECT * FROM category";
$records = mysql_query($query_records, $db) or die(mysql_error());
$records_array = mysql_fetch_array($records);

$final_array = implode(',', $records_array);
print_r($final_array);

yields this...

implode(): Bad arguments

    Maybe you need double quotes. The manual reference on implode lists an example this way:

    <?php
    
    $array = array('lastname', 'email', 'phone');
    $comma_separated = implode(",", $array);
    
    echo $comma_separated; // lastname,email,phone
    
    ?> 

      no, using double quotes ("") yields the same thing.

      Interestingly enough, using the mysql_fetch_assoc function instead of the mysql_fetch_array function in the above code works just fine, but a resource id number [catid] is returned in the resulting array and I don't want that to be included. I guess I could strip the catid out, I'm just perplexed as to why the other one doesn't work.

        okay, I think I figured out part of the problem. I actually had BOTH the mysql_fetch_array AND the mysql_fetch_assoc functions used against the same query result. When I moved the one above the other, the problem flip-flopped. I didn't post that part of the code above because I didn't think it had any affect on what I was trying to do. wrong.

        I'm still wondering how to return an array WITHOUT the the resource ID...

          I'm still wondering how to return an array WITHOUT the the resource ID...

          Yeah, so am I ! 😕

            Okay. I used the array_shift function to remove the resource id from the front of the array. I don't know if this is the most elegant way to go about it, but it seems to work so I can live with it!

            Here's the final version of what I'm using:

            $query_records = "SELECT * FROM category";
            $records = mysql_query($query_records, $db) or die(mysql_error());
            $records_array = mysql_fetch_assoc/B;

            $remove_catid = array_shift/B;
            $final_array = implode(',', $records_array);
            print_r($final_array);

              Write a Reply...