Ich bin in einem Team, das eine Android-Anwendung entwickelt, die sehr auf die Verwendung einer entfernten Datenbank angewiesen ist. Wir verwenden PhoneGap und Jquery Mobile und haben versucht, mit AJAX- und JSON-Aufrufen eine Verbindung zu unserer MySQL-Datenbank herzustellen. Momentan haben wir Probleme in unserer Testphase, nämlich zu verifizieren, dass wir überhaupt eine Verbindung haben, indem wir einen hartcodierten Benutzer von "Ted" aus mySQL/Eingabe über MySQL Workbench ziehen.Mit PhoneGap, AJAX und JQuery Mobile eine Verbindung zu einer MySQL-Datenbank herstellen
Von dem, was wir gesammelt haben, funktioniert der Prozess der Datenübertragung wie folgt aus:
Auf unserer HTML-Datei, haben wir ein
<script type="text/javascript" src="Connect.js"></script>
^Welche sollten das Connect.js Skript ausführen , richtig? Von da an wird Connect.js ausgeführt?
Connect.js läuft, verbindet es mit unserer ServerFile.php, die auf einem externen Web-Service gehostet wird, so dass es PHP zur Verbindung mit der MySQL-Datenbank ausführen und Informationen abrufen kann.
//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 script $.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 script 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 script 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'); }); });
Hier ist ServerFile.php. Dies sollte eine Verbindung zur MySQL-Datenbank herstellen, die Select-Anweisung ausführen und dann die Ausgabe an den Browser senden, der im JSON-Format codiert ist.
<?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));
}
?>
Doch ist hier nichts zeigt. Was machen wir von hier?
Haben Sie Debugging durchgeführt, um festzustellen, wo im Code das Problem liegt? Ich würde empfehlen, 'echo json_encode (array ('status' => 'success')); exit; 'beim Öffnen von ServerFile.php, um zu sehen, ob das Problem darin besteht, dass es irgendwo mit dem SQL blockiert wird, oder wenn es ein Problem beim Lesen ist, was zurückkommt. –
Oder eine andere Möglichkeit zum Debuggen wäre es, 'console.log (response);' nur innerhalb Ihrer $ .getJSON-Funktion zu setzen. Die Antwort wird dann in Ihrem Firebug angezeigt. –