How to create mysql query from titanium mobile

You must Login before you can answer or comment on any questions.

How to create mysql query from titanium mobile, is my question.

Does anyone have a lead to this?

Cheers.

— asked 4 years ago by Jae Lee
0 Comments

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.php

On 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

— answered 4 years ago by Gregor Munro
answer permalink
4 Comments
  • Greg, thanks for the great tip. Although I wasn't able to test out your code - due to the lack of mysqli class on my server, i will give it a shot.

    — commented 4 years ago by Jae Lee

  • Thank you sir for the great tutorial. I just got mine to work tonight.

    — commented 3 years ago by Mel Maxwell

  • Very helpfull thx ! But I don't understand something, maybe there is a mistake ?

    var json = json.cats;
        var pos;
        for( pos=0; pos < jsoncats.length; pos++){
            Ti.UI.info(json[pos].cat_name, json[pos].colour_name);
        }
    here, it's var json = json.cats or maybe var jsoncats = json.cats ?

    and also about the for :

    for( pos=0; pos < jsoncats.length; pos++){
            Ti.UI.info(json[pos].cat_name, json[pos].colour_name);
        }
    it's jsoncats.length or json.length

    Sorry I'm a begginer =/

    — commented 3 years ago by Jérémie Samson

  • Show 1 more comment

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

— answered 4 years ago by u no
answer permalink
1 Comment
  • could be drop me some lines of codes to be more specific? thx

    — commented 4 years ago by Jae Lee

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

Your Answer

Think you can help? Login to answer this question!