Titanium Community Questions & Answer Archive

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

Titanium, PHP and databases?

Heyup all,

Righteo sorry if I'm being dense and skipped over a bit of docu but I'm having a bit of problems with PHP in relation to Titanium.

Main problem is thus, is it possible to access databases through Titanium using PHP. I attempted to create a SQLITE database in a subfolder of my program and tried to connect to it but it told me the associated functions did not exist, fair enough. I then tried using MySQL functions to connect to a database but it occured to me wouldn't I need a username,host and password?

1: Is there documentation I've overlooked covering PHP-related topics?
2: Is it possible to use PHP to connect/use databases?
3: If so, in what format and is there any special information (default user/pass etc) associated with doing in this way?

Thanks in advance :)

— asked May 11th 2010 by Rupert Sharp
  • database
  • mysql
  • php
  • sqlite
  • titanium

4 Answers

  • Sure thing. using the code below you would received the data using the xhr.onload function. Check out the kitchen sink docs for xhr examples.

    xhr.onload is going to return responseText(among other objects) which is whatever data your .php page returns - in this case a json encoded array.

    not sure what experience you have in web languages but xhr is just ajax so all your doing is making an ajax call to a php page(which queries your database and returns an array that you will json encode) and returning it's data.

    var xhr = Titanium.Network.createHTTPClient();
    xhr.open('GET','http://www.foo.com/phpFile.php?varName='+varValue); // vars optional
    xhr.onload = function()
      data = JSON.parse(this.responseText);
      var dl = (data.length-1);
      for(i=0; i<=dl;i++)
        //should print some data
      } // end for loop
    }; // end xhr onload
    — answered May 11th 2010 by Kelly Redd
    1 Comment
    • Could I use xhr.open('GET','http://localhost/phpFile.php?varName='+varValue);?

      — commented July 11th 2010 by Peregrino Aleman
  • Hey, just create an XHR object and point to your php file. In that php file you'll do all your php just like normal - log in to db, query your db and return a json object.

    — answered May 11th 2010 by Kelly Redd
  • Thanks for your answer Kelly :) I'm still a bit confused though, could you go into a bit more depth, not entirely sure I understand.

    thanks :)

    — answered May 11th 2010 by Rupert Sharp
  • Hey again,
    Sorry for the late reply, been under the weather. Thanks for your reply however the problem I'm having is not returning the data to the page after accessing the database, it's accessing the database itself. The prob I'm having is how to access databases through PHP in Titanium, I know how to access a database through PHP but currently it appears that PHP in Titanium does not have SQLITE support and does not provide information about connecting to a MySQL DB (if any exists) or any other for that matter. I can't find any documentation on it either =/

    — answered May 17th 2010 by Rupert Sharp
    1 Comment
    • yeah.. me too searching for The docs regarding DB from PHP in titanium.. it seems that only Java Script is given more important and.. not the PHP

      I tried the following and i got uncaught exception

      $db = new SQLite3('dbname.db');

      but with MySQL it works fine provided MySQL Service is turned on..

      — commented April 11th 2012 by Packiya Rajan
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.