sql >> データベース >  >> RDS >> Mysql

phpで配列キーをチェックしてmysqlに配列値を保存する方法

    質問を正しく理解したと思います。

    ここで、コードのいくつかの行を削除できます(有効性チェックは含まれていません)

    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();
    }
    

    お役に立てば幸いです。

    更新された結果:

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

    データベースに問題はありません:

    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. MySQL:IN句のエントリに制限はありますか?

    2. MySQLDatagridviewでセルコンテンツをハイパーリンクに変換する

    3. mysqldbクエリから生の10進値を取得します

    4. 配列を使用して複数の行を更新する