sql result to javascript array

Solutions on MaxInterview for sql result to javascript array by the best coders in the world

showing results for - "sql result to javascript array"
Eduardo
13 Nov 2020
1<?php
2    //bind to $name
3    if ($stmt = $mysqli->prepare("SELECT category.name FROM category")) {
4        $stmt->bind_result($name);
5        $OK = $stmt->execute();
6    }
7    //put all of the resulting names into a PHP array
8    $result_array = Array();
9    while($stmt->fetch()) {
10        $result_array[] = $name;
11    }
12    //convert the PHP array into JSON format, so it works with javascript
13    $json_array = json_encode($result_array);
14?>
15
16<script>
17    //now put it into the javascript
18    var arrayObjects = <?php echo $json_array; ?>
19</script>
20