Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Cara memeriksa kunci array di php untuk menyimpan nilai array di mysql

Saya harap saya telah memahami pertanyaannya dengan benar.

di sini Anda dapat menghapus beberapa baris kode (tidak termasuk pemeriksaan validitas)

ini_set('display_errors', true);
try {
    $stmt = $db->prepare("insert into `<table>` (`id`,`message`) values(?,?);");
    $message_ids = array();
    $stories = array();
    foreach ($total_posts as $row) {
        if (isset($row['message'])) {
            // any record with 'message' is allowed
            // whenever has it 'story' or no
            $message_ids[] = $row['id'];
            $stories[]     = $row['message'];

        } else {
            if (isset($row['story'])) {
                // only story
            } else {
                // no message & no story
            }
        }
    }

    $message_id = implode("\n",$message_ids);
    $story      = implode("\n",$stories);
    $stmt->bind_param("ss", $message_id, $story);
    if ($stmt->execute()) {
        // success
    } else {
        // error handler
        $error = $stmt->error;
        // debug
        echo $error.PHP_EOL;
        die();
    }

    // or output to console
    // echo 'id: '.$message_id.PHP_EOL;
    // echo 'message: '.$story.PHP_EOL;
    // echo PHP_EOL;    
} catch (\Exception $e) {
    // debug
    echo $e->getMessage();
}

Semoga membantu.

Hasil yang diperbarui:

id: 10154521329397892_1015455584936789210154521329397892_1015454259720289210154521329397892_10154542027992892
msg: Insane!!!!!R.I.P JaihindJohnson & Johnson Finally Admits: Their Baby Products Contain Cancer-Causing Chemicals |

Saya tidak memiliki masalah dengan basis data:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0,00 sec)

mysql> use test;
Database changed

mysql> create table `test` (`id` varchar(128), `msg` varchar(128));
Query OK, 0 rows affected (0,10 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test           |
+----------------+
1 row in set (0,00 sec)

mysql> explain `test`;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | varchar(128) | YES  |     | NULL    |       |
| msg   | varchar(128) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0,01 sec)

mysql>

/// console
[65] $ php ./a.php
id: 10154521329397892_10154555849367892
10154521329397892_10154542597202892
10154521329397892_10154542027992892
msg: Insane!!!!!
R.I.P Jaihind
Johnson & Johnson Finally Admits: Their Baby Products Contain Cancer-Causing Chemicals |

 /// back to mysql shell
mysql> select * from `test`;
+-----------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
| id                                                                                                        | msg                                                                                                               |
+-----------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
| 10154521329397892_1015455584936789210154521329397892_1015454259720289210154521329397892_10154542027992892 | Insane!!!!!R.I.P JaihindJohnson & Johnson Finally Admits: Their Baby Products Contain Cancer-Causing Chemicals |  |
+-----------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)

mysql>



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bagaimana cara berhasil menulis ulang kode mysql-php lama dengan fungsi mysql_* yang tidak digunakan lagi?

  2. Pencadangan dan Pemulihan Sederhana untuk Database mysql dari Java

  3. Azure App Service ImportError:libmysqlclient.so.18:tidak dapat membuka file objek bersama:Tidak ada file atau direktori seperti itu

  4. Bagaimana cara menambahkan lokasi direktori ke mysql varchar?

  5. Cara elegan untuk menghapus baris yatim piatu?