I want to transform a php array into a javascript array.
When I try this code:
<?php
//db data
$verbindung = new mysqli($host_name, $user_name , $password, $database);
if ($mysqli->error) {
die('Verbindung schlug fehl:');
}
$sql= "SELECT Name FROM Zutaten;";
$result = mysqli_query($verbindung, $sql);
$zutatName = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$zutatName[] = $row['Name'];
}
print_r(array_values($zutatName));
?>
<script>
var zutatenArray = <?php echo json_encode($zutatName); ?>;
</script>
I get the following error messeage: "SyntaxError: expected expression, got ';' var zutatenArray = ;"
When I use this code:
<?php
//db data
$verbindung = new mysqli($host_name, $user_name , $password, $database);
if ($mysqli->error) {
die('Verbindung schlug fehl:');
}
$sql= "SELECT Name FROM Zutaten;";
$result = mysqli_query($verbindung, $sql);
$zutatName = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$zutatName[] = $row['Name'];
}
print_r(array_values($zutatName));
?>
<script>
var zutatenArray = [<?php echo json_encode($zutatName); ?>];
</script>
Then there is an empty javascript array. What am I missing?
Best regardds
print_r(array_values($zutatName));
print?