views:

101

answers:

3

This is php script that fetches a table values from mysql (one row). & echoes it as JSON

<?php  
      $username = "user";  
      $password = "********";  
      $hostname = "localhost";  
      $dbh = mysql_connect($hostname, $username, $password) or die("Unable to 
      connect to MySQL");  
      $selected = mysql_select_db("spec",$dbh) or die("Could not select first_test");  
      $query = "SELECT * FROM user_spec";  
      $result=mysql_query($query);     
      $outArray = array(); 
      if ($result) { 
      while ($row = mysql_fetch_assoc($result)) $outArray[] = $row; 
       } 
      echo json_encode($outArray);  
?> 

this is HTML file to receive & print json data.
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> //$('document').ready(function() {

    function Preload() {
    $.getJSON("http://localhost/conn_mysql.php", function(jsonData){  
    $.each(jsonData, function(i,j)
    { alert(j.options);});
    });} 

// });
    </script></head>

    <body onLoad="Preload()">
    </body>

</html> >
A: 

mysql_fetch_assoc will only return a single row from the database. You will need a loop to retrieve all rows:

$data = array();
while ($row = mysql_fetch_assoc($result)) {
    // add some or all of $row to the $data array
}
echo json_encode($data);
Richard Fearn
it happens same with ur solution too as with Fosco matter with little difference. The ouput of HTML is same i.e. the alert box ouput 'undefined'. While urs json echo in php output last 'options' value of the column while Fosco's json echo output the 1st 'options' value of the options column.
XCeptable
+3  A: 

Your PHP needs to actually put all the rows together:

$query = "SELECT * FROM user_spec"; 
$result=mysql_query($query);    
$outArray = array();
if ($result) {
  while ($row = mysql_fetch_assoc($result)) $outArray[] = $row;
}
echo json_encode($outArray);

Your Javascript needs to look at each of the rows..

$.getJSON("/whatever.php", function(jsonData) { 
   for (var x = 0; x < jsonData.length; x++) {
      alert(jsonData[x].options);
   }
});
Fosco
did u put for ech row in exact php syntax ?
XCeptable
@babar The `for each` is in the javascript code, not the PHP side.
Fosco
OH OK, thanX, I just made mistake......
XCeptable
r u sure javascript has for each loop ?
XCeptable
@babar did you try it?
Fosco
XCeptable
OK, its called a 'for in' loop in javascript } " But it opens an alert saying 'undefined' with ur code, not value is ouputted in HTML while php outputs [{"options":"smart_exp"}], 'smart_exp' is 1st value in 'options' column.
XCeptable
@babar I realized an issue with the php code and corrected it... try again. I will also add a different JS method.
Fosco
actually this does what I was expecting as I have done this earlier, it raises error, 'Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes) in D:\xampp\htdocs\conn_mysql.php on line 12'.
XCeptable
XCeptable
@babar it has to be $data[] = $row, otherwise you're only getting the last row... you see why if it's just $data = $row, it's no longer an array, right?
Fosco
$.getJSON with bew for loop is also not working, I tested it with while ($row = mysql_fetch_assoc($result)) loop in php that outputs all mysql values as I mentioned earlier.
XCeptable
@babar can you update your question with new code both php and js?.. I've also updated the php portion of my answer.
Fosco
I have updated the code, php is now showing all result in json echo but html is still not showing outpu. I am still working on it, may be some misplaced brace etc.
XCeptable
@babar are the html page and the php page on the same address? change your getJSON call to href just conn_mysql.php without the full URL.
Fosco
XCeptable
XCeptable
A: 

Hi, try this:

<?php 
    $username = "user"; 
    $password = "********"; 
    $hostname = "localhost"; 
    $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect 
         to MySQL"); 
    $selected = mysql_select_db("spec",$dbh) or die("Could not select first_test"); 
    $query = "SELECT * FROM user_spec"; 
    $result=mysql_query($query);

    $_ResultSet = array();

    while ($row = mysql_fetch_assoc($result)) {
       $_ResultSet[] = $row;
    }
       echo json_encode($_ResultSet); 
?>

and your jQuery would looks like :

$.getJSON("/yourscript.php", function(data) {
    $.each(data, function(i, j) {
        // use: j.columnName
    });
});

Good luck

Cybrix
I can't comment Fosco's answer but your example wont work if the query returns more then one row. Only the first one will be returned to getJSON.
Cybrix