mysql - Deliver rows to JavaScript using JSON -


i'm trying rows of mysql table arrays, haven't found out how correctly till now. appreciate if give me hint. it's very, wrong way i'm doing it, haven't found information regarding json , problem till now.

    function updatemap(){         var latlngarray = new array();         var titlearray = new array();         var ogvarray = new array();         var mp4array = new array();         var webmarray = new array();          $.getjson('getpoi.php', function(data){             for(var i=0; data.length; i++){                 latlngarray[i] = data[i][0];                  alert(latlngarray[i]);                 titlearray[i] = data[i][1];                 ogvarray[i] = data[i][2];                 mp4array[i] = data[i][3];                 webmarray[i] = data[i][4];             }         });     } 

the phpfile:

        $con = mysql_connect($server,$benutzername,$passwort) or          die ("keine verbindung moeglich");          mysql_select_db($datenbank, $con) or         die ("die datenbank existiert nicht");          $res = mysql_query("select * poi");         echo json_encode($res);         mysql_close($con); 

update: still doesn't work properly. alert function displays "0: [object object], 1: [object object], ..." guess didnt access data properly:

jsontest.html: <html>     <head>         <script src="jquery-1.6.1.js"></script>         <script type="text/javascript">             function updatemap(){                            $.getjson('getpoi.php', function(data) {                         $.each(data, function(key, value){                             alert(key + ': ' + value);                         });                     }                 );             }            </script>     </head>     <body onload="updatemap()">     </body> </html> 

getpoi.php:

<?php     $server = "localhost";     $benutzername = "user";     $passwort = "pw";         $datenbank = "d0116c39";      $con = mysql_connect($server,$benutzername,$passwort) or      die ("keine verbindung moeglich");      mysql_select_db($datenbank, $con) or     die ("die datenbank existiert nicht");      $allresults = array();      $res = mysql_query("select * poi");      while ($row = mysql_fetch_assoc($res)) {         $allresults[] = $row;     }      echo json_encode($allresults); ?> 

finally got it:

<html>     <head>         <script src="jquery-1.6.1.js"></script>         <script type="text/javascript">             function updatemap(){                            $.getjson('getpoi.php', function(data) {                         $.each(data, function(i, item){                             alert(data[i]['adresse']);                             alert(data[i]['titel']);                         });                     }                 );             }            </script>     </head>     <body onload="updatemap()">     </body> </html> 

calling :

$res = mysql_query("select * poi"); 

will execute query, not data : you must fetch it.


can done functions such mysql_fetch_assoc() (which has called once per row query has selected -- that's done in loop, typically)

each time fetch row, add array.

and, when you're done fetching rows , building array, json_encode() it, , echo resulting string client.


you'll end portion of code bit :

$allresults = array();  // execute query $res = mysql_query("select * poi");  // long rows, fetch them while ($row = mysql_fetch_assoc($result)) {     // , add them resulting array     $allresults[] = $row; }  // which, in end, can json-encoded echo json_encode($allresults); 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -