MySQL, how to insert



  • I have a mass of $test = [1], "Lalala," and another text. How am I supposed to put this mass in mysql cell and get back? Trying to do json_encode, not working.



  • It's best not to store the masses in the database cells, but to use separate cells for each line. But if I really want to,

    <?php
    

    $test = ["1", "Лалала", "И другой текст"];

    // записываем в базу данных
    $query = "INSERT INTO tbl VALUES (:data);";
    $stmt = $pdo->prepare($query);
    $stmt->execute([':data' => json_encode($test)]);

    // читаем из базы
    $query = "SELECT data FROM tbl;";
    $stmt = $pdo->prepare($query);
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    $result = json_decode($row['data'], true);

    print_r($result);

    https://phpize.online/?phpses=74d06d1211bb9c6a5f5dcc6a0b298eb1&sqlses=4e68a7394fef2c33e9886ef1ba7336d2&php_version=php8&sql_version=mysql57



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2