使用javascript获取php/mysql值
Using javascript to get php/mysql values
我正在考虑在appellerators titanium应用程序中构建一个android应用程序,我有一个问题,该应用程序的网站是使用php/mysql构建的,我想知道的是,钛只使用javascript, HTML和CSS,是否有一种方法我可以使用javascript动态地从数据库中提取数据?
如果这已经发布了,我很抱歉我搜索了,找不到它:S
使用PHP,获取数据库响应数组并像这样编码:
<?php
json_encode($db_array);
?>
更多信息:http://php.net/manual/en/function.json-encode.php
请注意,您需要PHP 5.2或以上版本才能使用PHP内置的JSON函数。
在Titanium中,您希望打开XHR(或网络处理程序)来获取数据:
var xhr = Ti.Network.createHTTPClient();
var.onload = function()
{
try
{
data = JSON.parse(this.responseText);
}
catch (excp)
{
alert('JSON parse failed');
}
// you should handle your network async, meaning you should handle any renders or any post elements here. if you make it sync, you'll cause all other handlers and functions to work improperly (like click events, etc).
}
xhr.open("GET", "your url here");
xhr.send();
您可以通过简单地调用data[0].some_col;
尝试阅读关于在Titanium应用程序中使用SQLite数据库的教程
抱歉这是针对iPhone的,但基本原理是一样的
http://mobile.tutsplus.com/tutorials/appcelerator/titanium-mobile-database-driven-tables-with-sqlite/
http://mobile.tutsplus.com/tutorials/appcelerator/titanium-mobile-database-driven-tables-with-sqlite-part-2/
http://mobile.tutsplus.com/tutorials/appcelerator/titanium-mobile-database-driven-tables-with-sqlite-%E2%80%93-part-3/
的用法如下:
var db = Ti.Database.install('../products.sqlite','products');
var rows = db.execute('SELECT DISTINCT category FROM products');
文档:http://developer.appcelerator.com/apidoc/mobile/1.3/Titanium.Database-module
最好的方法是使用JSON使用json_encode如果你从网站访问数据库。如果您尝试使用本地数据库,请使用sqlite
你需要在你的网站上建立一个web服务,并将数据与Titanium.Network.HTTPClient
你可以在这里看到:http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Network.HTTPClient-object
一个例子是:
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function() {
var data = this.responseText; //response is here
// do your thing within this scope, or call a function/event from here
}
var url = 'http://www.google.nl';
xhr.open("GET", url); // or "POST"
xhr.send();
注意,data变量在作用域之外是不可访问的,即使它是一个全局变量。在
范围内调用函数在应用程序中,你可以使用SQLite,或者不存储数据。
- HTML-使用mysql.php生成一个选择框.我怎样才能更改“;值“;生成的选择框选项的
- WebSQL作为通过MySQL / PHP的html5移动离线答案
- 根据先前的选择填充单个输入 - MYSQL PHP JS
- Google Maps API:如何替换源代码,从Google Spreadsheet API到MySQL/PHP
- Mysql php多选,排序
- LazyLoad从mysql/php-Json编码的数据和Jquery中加载内容
- MySQL/PHP/Javascript-回显文本中的撇号导致错误
- 我需要帮助将MySQL / PHP变量传递给JavaScript
- 使用MySQL,PHP和angular.js在数据库中插入数据数组
- 如何在同一页面上创建一个可点击的URL从mysql php结果
- 引导MYSQL PHP -发送模态内容
- 如何创建视图和获取数据mysql PHP javascript
- 点击,MySQL, PHP,关于交换图像
- 引导formvalidation.io远程验证器MySQL PHP
- 使用谷歌地图api与坐标从数据库javascript MySQL PHP创建线
- 如何上传android和iso应用程序包含mysql / php和包装与phonegap
- Jquery无限滚动使用Mysql/PHP反复循环
- 将表单组合中的数据保存到MySQL PHP数据库中
- 在MySQL / PHP上使用jQuery创建了动态页面
- 动态导航链接与Mysql, Php和Javascript