Spo*_*boy 5 javascript sql jquery jquery-mobile cordova
我是一个开发Android应用程序的团队,该应用程序将极大地依赖于远程数据库的使用.我们正在使用PhoneGap和Jquery Mobile,并尝试使用AJAX和JSON调用连接到我们的MySQL数据库.目前,我们在测试阶段遇到了麻烦,即通过MySQL Workbench从mySQL /输入中提取"Ted"的硬编码用户来验证我们甚至可以建立连接.
根据我们收集的信息,数据传输过程如下:
在我们的html文件中,我们有一个
<script type="text/javascript" src="Connect.js"></script>
Run Code Online (Sandbox Code Playgroud)
^哪个应该运行Connect.js脚本,对吗?那么,从那里开始运行Connect.js?
Connect.js运行,将其连接到托管在外部Web服务上的ServerFile.php,允许它运行PHP以连接到MySQL数据库并提取信息.
//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');
});
});
Run Code Online (Sandbox Code Playgroud)这是ServerFile.php.这应该连接到MySQL数据库,生成Select语句,然后将输出发送到以JSON格式编码的浏览器.
<?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));
}
?>
Run Code Online (Sandbox Code Playgroud)
但这里没有任何表现.我们从这里做什么?
首先第一件事。尝试确定问题出在哪里,服务器端还是客户端。
打印出您的数据库查询和编码的 json 可能很有用。如果您正在创建一个简单的 API 服务,您应该能够输入http://www.WEBSITENAME.com/ServerFile.php并查看输出如何。
使用 echo 来打印 php 的内容。
如果一切看起来都正常,那么就可以在 JavaScript 中打印出从服务器收到的响应,看看有什么问题。
使用 console.log 通过 javascript 打印内容。日志应该出现在 eclipse 的 logcat 部分中(因为您正在开发 Android 应用程序)
| 归档时间: |
|
| 查看次数: |
18741 次 |
| 最近记录: |