Storing contents of a PHP array in a MySQL database

前端 未结 3 1797
清酒与你
清酒与你 2021-01-24 03:24

I am having trouble storing array contents in mysql I am using the code below

foreach($found_entry as $key => $sd) {
         echo \"$key => $sd 
3条回答
  •  无人及你
    2021-01-24 03:37

    Adding data to MySQL with a PHP Array


    Inserting data into MySQL

    In your code you are performing an insert for every field.

    However, in most cases you know the field names before hand and so you can do a single INSERT with all of the fields.

    $sql = 'INSERT INTO tbl_name (field_a,field_b,field_c) VALUES('.$found_entry['field_a'].','.$found_entry['field_b'].','.$found_entry['field_c'].');';
    

    This cuts down on the number of queries required and so makes your code run faster and use less resources.

    Default values

    You can pass NULL when a value is not known and the DB should insert a default value.

提交回复
热议问题