Mysql
 sql >> Base de données >  >> RDS >> Mysql

Comment vérifier les clés du tableau dans php pour stocker les valeurs du tableau dans mysql

J'espère avoir bien compris la question.

ici vous pouvez supprimer quelques lignes de code (pas de contrôle de validité inclus)

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

J'espère que ça aide.

Résultats mis à jour :

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

Je n'ai aucun problème avec la base de données :

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>