-1

I'm trying to store a JSON-encoded array into MySql, but I get an error 1064.

My table structure:

enter image description here

And my code:

public static function addFriends($player, $friends = []){
    $array = json_encode($friends);
    $sql = "INSERT INTO friends (friends) VALUES ('$array') WHERE name = '" . $player . "'";
    $query = DatabaseManager::getConnection()->query($sql);
    if(!$query){
        echo mysqli_errno(DatabaseManager::getConnection());
        return;
    }
    echo 'Done';
}

The $friends array is like ["Car", "Plane", "Bug"].

plamut
  • 3,085
  • 10
  • 29
  • 40
GuyWhoDoThings
  • 85
  • 1
  • 1
  • 5
  • You want to insert only one record with the field `friends` having the array values in form of the string `Car, Plane, Bug`? Or you want to insert a record for each array item? –  Jan 14 '18 at 16:43
  • 2
    Can you provide the **full** error message? – Nico Haase Jan 14 '18 at 16:44
  • 1
    http://php.net/manual/en/mysqli.prepare.php – Paul Spiegel Jan 14 '18 at 16:49
  • @NicoHaase How to get it? mysqli_errno display only 1064, nothing else – GuyWhoDoThings Jan 14 '18 at 16:59
  • Put this two lines in your file (on top of it) where you call the function `addFriends`: `error_reporting(E_ALL);` followed by `ini_set('display_errors', 1);`. This way you will be able to see the whole error on screen. –  Jan 14 '18 at 17:03
  • 1
    https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments – Paul Spiegel Jan 14 '18 at 17:05

1 Answers1

0

Try this:

$sql = "INSERT INTO friends (friends) VALUES ('".$array."') WHERE name = '" . $player . "'";
blanck
  • 1