I have a MySQL query that I pass to a PHP array. The PHP array turns into a JSON array using json_encode . I am trying to do this for printing on my html page. Here is what I still have.
<?php class db { public $conn; function dbLogin() { $this->conn = new mysqli("1.2.3.4","user","pwd","db"); } public function selectQuery() { $this->dbLogin(); $query = " SELECT * FROM tekken_7_frame_data "; echo "<pre>"; $resultArray = Array(); if ($result = $this->conn->query($query)) { while ($row = $result->fetch_array()) { $resultArray[] = $row; } } echo "</pre>"; $resultJson = json_encode($resultArray); } } $fData = new db(); $fData->selectQuery(); ?> <html> <head> </head> <body> <script> var jsonArray = <?php echo $resultJson ?>; document.write(jsonArray); for (i = 0; i < jsonArray.length; i++) { document.write(jsonArray[i][1]); } </script> </body> </html>
I read other similar questions in StackOverflow with no luck; my html page is completely empty. I tried to put json_econde(...) inside the javascript jsonArray variable instead of my PHP class. That didn't work either. I also have a var_dump 'd PHP array without any problems (all this is displayed on the page), and also put the array in a PHP variable using json_encode , and then echoed this array without any problems.
I do not know how to access this JSON array. I ultimately want it in a table, but since this is my first time bridging the gap between PHP and Javascript, I decided that I would take it one step at a time to know exactly what was going on.
json javascript arrays php mysql
aCarella
source share