jquery ajax连接mysql_javascript – 使用PhoneGap,AJAX和JQuery Mobile连接到MySQL数据库

我是一个开发

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('') + '
').trigger('create');

});

});

这是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));

}

?>

但这里没有任何表现.我们从这里做什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值