I am on a team developing an Android application that will rely heavily on using a remote database. We use PhoneGap and JQuery Mobile and try to connect to our MySQL database using AJAX and JSON calls. We are currently having problems in our test phase, which is to verify that we even have a connection, pulling the hard-coded user "Ted" out of mySQL / input through the MySQL Workbench.
From what we have compiled, the data transfer process works as follows:
In our html file we have
<script type="text/javascript" src="Connect.js"></script>
^ Which Connect.js script should work, right? So, does Connect.js start from there?
Connect.js starts by connecting it to our ServerFile.php hosted on an external web service, allowing it to run PHP to connect to the MySQL database and retrieve information.
//run the following code whenever a new pseudo-page is created $('#PAGENAME').live('pageshow', function(event)) { // cache this page for later use (inside the AJAX function) var $this = $(this); // make an AJAX call to your PHP $.getJSON('http://www.WEBSITENAME.com/ServerFile.php', function (response) { // create a variable to hold the parsed output from the server var output = []; // if the PHP returned a success if (response.status == 'success') { // iterate through the response rows for (var key in response.items) { // add each response row to the output variable output.push('<li>' + response.items[key] + '</li>'); } // if the PHP returned an error } else { // output an error message output.push('<li>No Data Found</li>'); } // append the output to the `data-role="content"` div on this page as a // listview and trigger the `create` event on its parent to style the // listview $this.children('[data-role="content"]').append('<ul data-role="listview">' + output.join('') + '</ul>').trigger('create'); }); });
Here is ServerFile.php. This should connect to the MySQL database, make a Select statement, and then send the output to a browser encoded in JSON format.
<?php //session_start(); $connection = mysql_connect("csmadison.dhcp.bsu.edu", "clbavender", "changeme"); $db = mysql_select_db("cs397_clbavender", $connection); //include your database connection code // include_once('database-connection.php'); //query your MySQL server for whatever information you want $query = mysql_query("SELECT * FROM Users WHERE Username ='Ted'", $db) or trigger_error(mysql_error()); //create an output array $output = array(); //if the MySQL query returned any results if (mysql_affected_rows() > 0) { //iterate through the results of your query while ($row = mysql_fetch_assoc($query)) { //add the results of your query to the output variable $output[] = $row; } //send your output to the browser encoded in the JSON format echo json_encode(array('status' => 'success', 'items' => $output)); } else { //if no records were found in the database then output an error message encoded in the JSON format echo json_encode(array('status' => 'error', 'items' => $output)); } ?>
But nothing is visible here. What are we doing here?
source share