views:

90

answers:

2

I have a php file that receives data from mySQL table. The mySQL table 'user_spec' has only one field 'options' that it returns. Then I convert returned data into JSON, under is code doing that.

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

then in an HTML file, I try to output data by this piece of code But it is not working. I will be very thankful for any help.

<html>
    <head>
    <script type="text/javascript"   
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript" language="javascript">
    function Preload() {
    $.getJSON("Dhttp://localhost/conn_mysql.php", function(json){
    alert("JSON Data: " + json.user_spec);
    });}

    </script></head>
    <body onLoad="Preload()">
    </body>
    </html> 
A: 
  1. Jquery missing? I assume you gave us a sample of your HTML file and that you have a reference to the jquery script on the page.
  2. Syntax error? as Felix wrote - use console of firebug or similar to detect errors.
  3. PHP error? try to get the json response using a browser - see you got a response.
  4. Address error? try using absolute link instead of the relative link

Hope this gives you some options to narrow down the issue you arefacing.

Dror
sorry, reference was missing. I am using notepad so no console but now I will start using firebug OR some similar. Yes, absolute link solve the problem to this far.
XCeptable
A: 

I think instead of using the file adress: "D:xampp/htdocs/conn_mysql.php" you must use an url defined by xampp such as "http://localhost/mytest/conn_mysql.php"

Another thing you need to look out is the method. $.getJSON (http://api.jquery.com/jQuery.getJSON/), as its name says, works with the GET method. Maybe you should try $.post or $.ajax (http://api.jquery.com/jQuery.post/).

Oh! And to start your script, not all the browsers supports the <body onload="">. Furthermore, it waits for the page to be loaded, not the DOM, what can give you problems with your scripts sometimes. You should use $('document').ready(function(), that waits for the DOM to be loaded. This way:

<script type="text/javascript">
$('document').ready(function()
{

    $.getJSON("D:xampp/htdocs/conn_mysql.php", function(json){
    alert("JSON Data: " + json.options);

});
</script>

I hope it can be useful! ^^

Jayme
XCeptable
You´re welcome! ;DUse this function to get all the data: mysql_fetch_array().Here is the documentation: http://php.net/manual/en/function.mysql-fetch-array.phpFor handling the data you'll need to use $.each() or something similar, but it's almost there! ;D
Jayme