Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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.

— asked August 9th 2010 by Jae Lee
  • json
  • mysql
  • rest
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 August 9th 2010 by Gregor Munro
    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 August 12th 2010 by Jae Lee
    • Thank you sir for the great tutorial. I just got mine to work tonight.

      — commented August 3rd 2011 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 April 11th 2012 by Jérémie Samson
    • Thank you for this

      — commented August 21st 2012 by Chuck Ezuma
  • 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??

    — answered July 29th 2011 by Rajaa Samaha
    permalink
    1 Comment
    • What kind of error message you got?

      — commented August 3rd 2011 by Mel Maxwell
  • 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 August 9th 2010 by u no
    permalink
    1 Comment
    • could be drop me some lines of codes to be more specific? thx

      — commented August 9th 2010 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

    — answered August 3rd 2011 by Rajaa Samaha
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.