我是一个开发
Android应用程序的团队,该应用程序将极大地依赖于远程数据库的使用.我们正在使用PhoneGap和Jquery Mobile,并尝试使用AJAX和JSON调用连接到我们的MySQL数据库.目前,我们在测试阶段遇到了麻烦,即通过MySQL Workbench从mySQL /输入中提取“Ted”的硬编码用户来验证我们甚至可以建立连接.
根据我们收集的信息,数据传输过程如下:
>在我们的html文件中,我们有一个
^哪个应该运行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('
' + response.items[key] + '');}
// if the PHP script returned an error
} else {
// output an error message
output.push('
No Data Found');}
// 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('
- ' + output.join('') + '
});
});
这是ServerFile.php.这应该连接到MySQL数据库,生成Select语句,然后将输出发送到以JSON格式编码的浏览器.
//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));
}
?>
但这里没有任何表现.我们从这里做什么?