How to create mysql query from titanium mobile
How to create mysql query from titanium mobile, is my question.
Does anyone have a lead to this?
Cheers.
4 Answers
-
Accepted Answer
Hi Jae Lee
Ok, so you need two components, a client and a server.
In this example I will retrieve two different fields from the same table; a catalog name and colour via an HTTP GET.
On the client ( ie in Titanium ):
var xhr = Titanium.Network.createHTTPClient(); xhr.onload = function(){ var json = JSON.parse(this.responseText); if (!json) { Titanium.API.info('Error - Null return!'); return; } var json = json.cats; var pos; for( pos=0; pos < jsoncats.length; pos++){ Ti.UI.info(json[pos].cat_name, json[pos].colour_name); } }; xhr.open('GET', <server_url+php_uri>); xhr.send();
where <server_url+php_uri> is the url of your server side php script:
eg
http://www.myserver.com/catalog.phpOn the server side, you need to have created your database, installed Apache, PHP, MySQL, mysqli and all that. I wont cover this here.
Fill in the <databaseuser>… etc with the database specifics for your web server and database server.
<?php $mysqli = new mysqli("localhost","<databaseuser>","<databasepassword>","<databasename>"); if (mysqli_connect_errno()) { printf("Can't connect to SQL Server. Error Code %s\n", mysqli_connect_error($mysqli)); exit; } // Set the default namespace to utf8 $mysqli->query("SET NAMES 'utf8'"); $json = array(); if($result = $mysqli->query("select * from cats")) { while ($row=$result->fetch_assoc()) { $json[]=array( 'cat_name'=>$row['cat_name'], 'cat_colour'=>$row['colour_name'] ); } } $result->close(); header("Content-Type: text/json"); echo json_encode(array( 'cats' => $json )); $mysqli->close(); ?>
Hope this helps
Greg -
hello im right in xhr.open('GET', <server_url+php_uri>);
loginReq.open('POST','http://localhost/titanium/post_auth.php'); but it's not work!!!
can u help me plz?? -
do you want to make a real mysql query on port 3306 of a mysql server? that'd mean that you have to implement a mysql-client in javascript, with socket connections etc.
i'd put the mysql logic in a php script serverside, open a xhr request to it and return a JSON string, that's easy and works.
–u
-
i'm right xhr.onerror and i'm right an alert in this fonction the alert in this function always appear so the url cannot access bye titanium.i'm so tired to this probleme help me please