3

I need to insert multiple array. I get error: mysql_real_escape_string() expects parameter 1 to be string, array given

My example code:

 $array_info is 
    Array
(
[0] => Array
    (
        [0] => a
        [1] => b
        [2] => c
    )

[1] => Array
    (
        [0] => d
        [1] => e
        [2] => f
    )

)

 $data = array(
        'blog_id' => NULL,
        'post_id' => $id,
        'ing' => $array_info
        );
        $insert = $wpdb->insert($table_name, $data);
6
  • What is your $table_name value?
    – birgire
    Commented Mar 25, 2013 at 9:05
  • This value is correct wp_tablename
    – andys
    Commented Mar 25, 2013 at 11:41
  • 1
    Per the Codex page on WPDB, $wpdb->insert() expects $data to be an array of column => value pairs. Your $data array doesn't meet that criterion.
    – Pat J
    Commented Mar 25, 2013 at 13:48
  • You provided no context to your question, it's not clear why you are doing this, or what the purpose is, as such it is very hard to determine what a correct answer would actually be. Also we do not see the code that generates array_info
    – Tom J Nowell
    Commented Mar 25, 2013 at 16:26
  • Also, array_info is an array, not a string, as the error says
    – Tom J Nowell
    Commented Mar 25, 2013 at 16:27

1 Answer 1

2

What is your table structure. Please be aware that a $wpdb->insert call translate the array to an sql statement of the form

INSERT INTO table_name VALUES (value1, value2, value3,...)

Since there is no such thing as an array type for colums in sql, you will need to either serialize any subarray or make a seperate field for each item in the sub array (and move items from the subarray to the main array).

Please be aware that it is generally not a good practice to have structured data inside a SQL field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.