Connect to database online

Is there anybody who has got a working example of how to connect to a database online?

I have tried this:http://www.burtonsmediagroup.com/blog/2012/11/how-to-download-json-data-from-mysql-using-corona-sdk/ but I didn´t get it to work.

I have downloaded Lita and created a database but it looks like it does´t do the job, is it better to create a database in php?

I am a bit confused here and hope that someone can help me with this one :slight_smile:

//Magnus

Well that example uses PHP, so I would suggest that if you want to use it then it would probably be best to use PHP as well. 

I’ve never used Lita so can’t comment on that.

I use MySQLWorkbench to create and administrate my database, and then PHP script on my server to access it.

Most basic MySQL + PHP tutorials will look very similar to the one you posted:

http://www.phphaven.com/article.php?id=65

http://codular.com/php-mysqli

I can now connect to the database.

The database has 4 entries just for testing.

This is the code I am using:

\<?php // CONNECT TO THE DATABASE $DB\_NAME = 'xxxxx'; $DB\_HOST = 'localhost'; $DB\_USER = 'xxxxx'; $DB\_PASS = 'xxxxx'; $mysqli = new mysqli($DB\_HOST, $DB\_USER, $DB\_PASS, $DB\_NAME); if (mysqli\_connect\_errno()) { printf("Connect failed: %s\n", mysqli\_connect\_error()); exit(); } $query = "SELECT \* FROM `news`"; $result = $mysqli-\>query($query) or die($mysqli-\>error.\_\_LINE\_\_); $json = array(); if(mysql\_num\_rows($result)) { while($row=mysql\_fetch\_row($result)) { $json[]=$row; } } echo 'Total results: ' . $result-\>num\_rows; echo json\_encode($json); mysql\_close($DB\_NAME); ?\>

The output I get is:

Total results: 4[]

It says that there are 4 entries but the content is missing.

I got it, it was a classic typo  :smiley:

Well that example uses PHP, so I would suggest that if you want to use it then it would probably be best to use PHP as well. 

I’ve never used Lita so can’t comment on that.

I use MySQLWorkbench to create and administrate my database, and then PHP script on my server to access it.

Most basic MySQL + PHP tutorials will look very similar to the one you posted:

http://www.phphaven.com/article.php?id=65

http://codular.com/php-mysqli

I can now connect to the database.

The database has 4 entries just for testing.

This is the code I am using:

\<?php // CONNECT TO THE DATABASE $DB\_NAME = 'xxxxx'; $DB\_HOST = 'localhost'; $DB\_USER = 'xxxxx'; $DB\_PASS = 'xxxxx'; $mysqli = new mysqli($DB\_HOST, $DB\_USER, $DB\_PASS, $DB\_NAME); if (mysqli\_connect\_errno()) { printf("Connect failed: %s\n", mysqli\_connect\_error()); exit(); } $query = "SELECT \* FROM `news`"; $result = $mysqli-\>query($query) or die($mysqli-\>error.\_\_LINE\_\_); $json = array(); if(mysql\_num\_rows($result)) { while($row=mysql\_fetch\_row($result)) { $json[]=$row; } } echo 'Total results: ' . $result-\>num\_rows; echo json\_encode($json); mysql\_close($DB\_NAME); ?\>

The output I get is:

Total results: 4[]

It says that there are 4 entries but the content is missing.

I got it, it was a classic typo  :smiley: