Mysql
 sql >> Baza danych >  >> RDS >> Mysql

Jak sprawdzić klucze tablicy w php, aby przechowywać wartości tablicy w mysql

Mam nadzieję, że dobrze zrozumiałem pytanie.

tutaj możesz usunąć niektóre linijki kodu (bez sprawdzania poprawności)

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

Mam nadzieję, że to pomoże.

Zaktualizowane wyniki:

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

Nie mam problemów z bazą danych:

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. Przesyłanie obrazu PHP do MySQL nie działa

  2. Mac OS X — błąd środowiska:nie znaleziono mysql_config

  3. SQL GROUP_CONCAT podzielony na różne kolumny

  4. Dołączenie lewe przy użyciu kryteriów hibernacji

  5. Neo4j - Wybieranie danych za pomocą MATCH za pomocą Cypher