January 01 2014

Tidesdk connect to MySQL Database

Tagged Under :

In here, I am showing you how to use TideSDK to connect MySQL Database with PHP in your applicaion.

Example Data
The example data in this post uses as below.
CREATE TABLE fruits (`id` int, `name` varchar(50));
INSERT INTO fruits (`id`, `name`)
VALUES (1, 'Apple'),
       (2, 'Durian'),
       (3, 'Banana'),
       (4, 'Lemon'),
       (5, 'Pear'),
       (6, 'Star fruit'),
       (7, 'Strawberry'),
       (8, 'Orange');

After that we need create a connection string with the Database. Save the code below as conn.php and put it inside the Resources folder.
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('foo', $conn);

Now, we create another file and the name is index.php. Remember the index.php and conn.php was put in same directory in this example. It was because below example code need include the conn.php.

Copy code below to index.php and then save it.
$sql = "SELECT * FROM fruits";
$results = mysql_query($sql);

echo "<h2>Fruits</h2>";
$i = 1;
while ($row = mysql_fetch_array($results)) {
  echo $i.". ".$row[1]."<br/>";

Before launch the application, remember update the “url” to “app://index.php“, inside the “tiapp.xml” file.


Make a Comment