I'm trying to pass on a PHP array to then use the array in JavaScript.
The PHP code I'm using is as follows:
<?php
$link = mysqli_connect("localhost", "root", "password", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM Employees";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
print_r($row);
/* free result set */
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
//convert the PHP array into JSON format, so it works with javascript
$json_array = json_encode($data);
?>
JavaScript:
<script>
var array = <?php echo $data; ?>;
console.log(array);
</script>
The data array in PHP doesn't seem to get passed on to the Javascript var array. When looking at the console on firebug the following error messages are displayed:
Notice - Array to string conversion.
I'd really appreciate any help as to why this error is occurring.
echo $json_array;notecho $data;<?php echo $json_array; ?>?