MySQL CAST function not working and outputted as column name

I have a mysql query like this:

$results = $wpdb->get_results($wpdb->prepare(
               "
               (SELECT 
                CAST(active AS SIGNED)
                FROM $table_name
                WHERE user_id = %d)
                ", $user_id
        ), ARRAY_A); // Output query as array

I then output the result with json_encode, however, the result is outputted like this:

Read More
{"CAST(active AS SIGNED)":"1"}

It shows the function name as column name and the value is still a string.
Any ideas what’s going on ? Thanks for your suggestions.

Related posts

1 comment

  1. Try changing your SQL adding an “as” statement. Like this:

    (SELECT 
     CAST(active AS SIGNED) as column
     FROM $table_name
     WHERE user_id = %d)
    

    Then you can fetch your data using the name “column”.

Comments are closed.