sql >> Databasteknik >  >> RDS >> Mysql

Hur man kontrollerar arraynycklarna i php för att lagra arrayvärden i mysql

Jag hoppas att jag har förstått frågan rätt.

här kan du ta bort några rader kod (ingen giltighetskontroll ingår)

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

Hoppas det hjälper.

Uppdaterade resultat:

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

Jag har inga problem med databasen:

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 5.7-fel (1093:Du kan inte ange måltabell ___ för uppdatering i FROM-klausul) - vanlig lösning fungerar inte

  2. MySQL subquery - Hitta endast första posten i en LEFT JOIN

  3. Generera en tabellrad med <td>-värden från DB PHP

  4. SQL-kommando mellan två datumtider för bordsreservation