PHP Warning: Invalid argument supplied for foreach() error non stop

Viewed 23

I am connecting to a local database and want to store the values of a json into it, the current code i have is:

    <?php
$servername = 'localhost';
$username = 'username';
$password = 'pass';
$databaseName = 'databasename';
    $con = new mysqli($servername,$username,$password,$databaseName);
    if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
} 
echo "Connected successfully\n";
    $jsondata = file_get_contents('prices.json');
    $data = json_decode($jsondata, true);

    foreach ($data as $name) {
        $price = ($name['buff163']['starting_at']['price'] * 0.141279);
        $sql = "INSERT INTO prices(name, price) VALUES('$name', '$price')";
}
?>

Although with any changes i have tried to make i get the same error

PHP Warning:  Invalid argument supplied for foreach() in /var/scripts/priceupdate.php on line 14

What am i doing wrong? it seems to be working for other people.

1 Answers

The solution to the problem was shown by @barmar that the issue was not in the code but the stored json file which was not correctly set up.

Related