Jump to content

jQuery returned Data is NULL


mjsulliv

Recommended Posts

Hello all. I’ve been trying to use jQuery to get data from a DB and javascript to use the data to build the output and show it. If call a test file with a JSON object, it works. If I query a DB the data sent back to the js callback-function is always NULL. I have a small example below of the three files involved in the project, the starting index file, the called php that reads the DB and the SQL that builds the table.I’ve been beating this up since mid week and can’t seem to find a solution. Any help would be appreciated.index.php:

 <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>	<title>JSON Test</title>	<script type='text/javascript'  src="http://code.jquery.com/jquery-latest.js"></script> 	<script type='text/javascript'>	  $(document).ready(function (data) {		$.getJSON("getDbData.php", {		  "tableName": "test_tbl"		},		processResult);	  });	  function processResult(data){		for(property in data){		  $("#output").append("<h2>" + property + " is: " + data[property] + "</h2>");		}	  }	</script>  </head>  <body>	<h1>Test of JSON Req to DB</h1>	<div id = "output"></div>  </body></html>

getDbData.php:

<?php$connection = mysql_pconnect("localhost", "root", "") or die("couldn't connect to server");mysql_select_db("test", $connection);$tableName = $_REQUEST["tableName"];$sql = "SELECT * FROM $tableName";$resultList = mysql_query($sql, $connection);$row = mysql_fetch_array($resultList, MYSQL_ASSOC);$data = json_encode($row); // just used to look at structue of "JSON" datareturn $data;?>

SQL:

-- phpMyAdmin SQL Dump-- version 3.2.4-- http://www.phpmyadmin.net---- Host: localhost-- Generation Time: Jun 10, 2011 at 07:32 AM-- Server version: 5.1.41-- PHP Version: 5.3.1SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";---- Database: `test`------ Table structure for table `test_tbl`--DROP TABLE IF EXISTS `test_tbl`;CREATE TABLE IF NOT EXISTS `test_tbl` (  `name` varchar(10) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;---- Dumping data for table `test_tbl`--INSERT INTO `test_tbl` (`name`) VALUES('mike');

Link to comment
Share on other sites

Hello all. I’ve been trying to use jQuery to get data from a DB and javascript to use the data to build the output and show it. If call a test file with a JSON object, it works. If I query a DB the data sent back to the js callback-function is always NULL. I have a small example below of the three files involved in the project, the starting index file, the called php that reads the DB and the SQL that builds the table.I’ve been beating this up since mid week and can’t seem to find a solution. Any help would be appreciated.index.php:
 <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>	<title>JSON Test</title>	<script type='text/javascript'  src="http://code.jquery.com/jquery-latest.js"></script> 	<script type='text/javascript'>	  $(document).ready(function (data) {		$.getJSON("getDbData.php", {		  "tableName": "test_tbl"		},		processResult);	  });	  function processResult(data){		for(property in data){		  $("#output").append("<h2>" + property + " is: " + data[property] + "</h2>");		}	  }	</script>  </head>  <body>	<h1>Test of JSON Req to DB</h1>	<div id = "output"></div>  </body></html>

getDbData.php:

<?php$connection = mysql_pconnect("localhost", "root", "") or die("couldn't connect to server");mysql_select_db("test", $connection);$tableName = $_REQUEST["tableName"];$sql = "SELECT * FROM $tableName";$resultList = mysql_query($sql, $connection);$row = mysql_fetch_array($resultList, MYSQL_ASSOC);$data = json_encode($row); // just used to look at structue of "JSON" datareturn $data;?>

SQL:

-- phpMyAdmin SQL Dump-- version 3.2.4-- http://www.phpmyadmin.net---- Host: localhost-- Generation Time: Jun 10, 2011 at 07:32 AM-- Server version: 5.1.41-- PHP Version: 5.3.1SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";---- Database: `test`------ Table structure for table `test_tbl`--DROP TABLE IF EXISTS `test_tbl`;CREATE TABLE IF NOT EXISTS `test_tbl` (  `name` varchar(10) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;---- Dumping data for table `test_tbl`--INSERT INTO `test_tbl` (`name`) VALUES('mike');

return does not mean print/echo return is used to return a value from a function, which could be set to a variable or print/echo outed
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...