javascript - Show database records with json -


i building simple app intel xdk , need connect app mysql database. because intel xdk not allow php files needed connect html php via javascrapt , json.

here php code found , edited sends php array json object intel xdk app:

    <?php  if(isset($_get["get_rows"])) {     //checks format client wants     if($_get["get_rows"] == "json")     {         $link = mysqli_connect("localhost", "xxxx", "xxxx", "xxxx");          /* check connection */         if (mysqli_connect_errno()) {             echo mysqli_connect_error();             header("http/1.0 500 internal server error");             exit();         }          $query = "select * quotes limit 1";          $jsondata = array();          if ($result = mysqli_query($link, $query)) {              /* fetch associative array */             while ($row = mysqli_fetch_row($result)) {                 $jsondata = $row;             }              /* free result set */             mysqli_free_result($result);              //encode json format              echo  "<h1>" . json_encode($jsondata) . "</h1>";          }         else {             echo  "<h1>" . json_encode($jsondata) . "</h1>";         }          /* close connection */         mysqli_close($link);     }     else     {         header("http/1.0 404 not found");     } } else {     header("http/1.0 404 not found"); }  ?> 

my index.html has following code:

<head>     <script>                 function getquote() {                 var xmlhttp;                 if (window.xmlhttprequest)                   {// code ie7+, firefox, chrome, opera, safari                   xmlhttp=new xmlhttprequest();                   }                 else                   {// code ie6, ie5                   xmlhttp=new activexobject("microsoft.xmlhttp");                   }                 xmlhttp.onreadystatechange=function()                   {                   if (xmlhttp.readystate==4 && xmlhttp.status==200)                     {                     document.getelementbyid("quote").innerhtml=xmlhttp.responsetext;                     }                   }                 xmlhttp.open("get", "http://domain/db.php?get_rows=json", false);                 xmlhttp.send();                 }     </script>         </head>      <body onload="getquote()">          <div id="quote">     </div>      </body> 

my code working , app shows latest row in table. problem code returns entire row someting on screen: ["column1","column2"]. need "column1" record , "column2" record.

anyone can me? i've been working on problem entire day , can't find solution.. thanks!


this code have now:

<?php  if(isset($_get["get_rows"])) {     //checks format client wants     if($_get["get_rows"] == "json")     {         $link = mysqli_connect("localhost", "xxx", "xxxx", "xxxx");          /* check connection */         if (mysqli_connect_errno()) {             echo mysqli_connect_error();             header("http/1.0 500 internal server error");             exit();         }          $query = "select quote, author quotes id = " . date('d');          $jsondata = array();          if ($result = mysqli_query($link, $query)) {              /* fetch associative array */         $row = $result->fetch_assoc($result);          // create new array , assign column values          // can either turn associative array or basic array         $ret= array();          $ret[] = $row['quote'];          $ret[] = $row['author'];               //encode json format              echo  json_encode($ret);          }          else {              echo  json_encode($ret);         }          /* close connection */         mysqli_close($link);     }     else     {         header("http/1.0 404 not found");     } } else {     header("http/1.0 404 not found"); }   ?> 

i empty screen [null,null].. knows what's wrong code? tried debugging, new php , can't manage make work.. thanks!

try this

    $query = "select column1,column2 quotes limit 1";      $jsondata = array();      if ($result = mysqli_query($link, $query)) {          /* fetch associative array */         $row = $result->fetch_assoc($result);           // create new array , assign column values          // can either turn associative array or basic array         $ret= array();         $ret['name1'] = $row['column1'];          $ret['name2'] = $row['column2'];          // or can use, comment above 2 lines , uncomment following 2 lines.         //$ret[] = $row['column1'];          //$ret[] = $row['column2'];            //encode json format          echo  json_encode($ret); 

if find type error here, kindly share


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -