<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.webos-internals.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Anno</id>
	<title>WebOS Internals - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.webos-internals.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Anno"/>
	<link rel="alternate" type="text/html" href="http://wiki.webos-internals.org/wiki/Special:Contributions/Anno"/>
	<updated>2026-04-16T10:56:32Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.1</generator>
	<entry>
		<id>http://wiki.webos-internals.org/index.php?title=Talk:Tutorials_webOS_Getting_JSON_From_An_External_MySQL_Database&amp;diff=6770</id>
		<title>Talk:Tutorials webOS Getting JSON From An External MySQL Database</title>
		<link rel="alternate" type="text/html" href="http://wiki.webos-internals.org/index.php?title=Talk:Tutorials_webOS_Getting_JSON_From_An_External_MySQL_Database&amp;diff=6770"/>
		<updated>2009-10-31T04:09:35Z</updated>

		<summary type="html">&lt;p&gt;Anno: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I would '''''HIGHLY''''' suggest using this example as an example alone. You NEVER want to create a direct link to your database. Instead, you want to abstract it.&lt;br /&gt;
&lt;br /&gt;
Instead of accessing a table called users, you'd probably want to create a method for logging in, permissions, etc so no one can just view the contents of an entire table.&lt;br /&gt;
&lt;br /&gt;
In fact, I feel that this tutorial is quite negligent as it doesn't teach good practice at all.&lt;br /&gt;
&lt;br /&gt;
Remember, keep all logic that can retrieve data on the server ''only'' and never anywhere else. If you want users, you should &amp;quot;request&amp;quot; the users from the server and the server should determine what kind of data you're allowed to get.&lt;br /&gt;
&lt;br /&gt;
I have modified the previous &amp;quot;What's Next&amp;quot; step. I would suggest that someone, at some point, revises the example to use better coding practices rather than having a note saying &amp;quot;btw, this is bad, don't do what I just showed you&amp;quot;.&lt;/div&gt;</summary>
		<author><name>Anno</name></author>
	</entry>
	<entry>
		<id>http://wiki.webos-internals.org/index.php?title=Tutorials_webOS_Getting_JSON_From_An_External_MySQL_Database&amp;diff=6769</id>
		<title>Tutorials webOS Getting JSON From An External MySQL Database</title>
		<link rel="alternate" type="text/html" href="http://wiki.webos-internals.org/index.php?title=Tutorials_webOS_Getting_JSON_From_An_External_MySQL_Database&amp;diff=6769"/>
		<updated>2009-10-31T04:08:17Z</updated>

		<summary type="html">&lt;p&gt;Anno: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This tutorial covers a simple example of how to connect to a PHP page using an Ajax POST method to retrieve data from an external MySQL datbase and return the results in JSON format.&lt;br /&gt;
&lt;br /&gt;
==What You Need==&lt;br /&gt;
===Required===&lt;br /&gt;
*A Web server with MySQL&lt;br /&gt;
*Mojo SDK and emulator (obvious)&lt;br /&gt;
===Optional===&lt;br /&gt;
*Palm Pre&lt;br /&gt;
==The Setup==&lt;br /&gt;
===MySQL===&lt;br /&gt;
The first thing you need to do is create a MySQL database with at least one table in it.  This tutorial will focus on a single table (named &amp;lt;strong&amp;gt;users&amp;lt;/strong&amp;gt;) with the following structure:&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; style=&amp;quot;border-color:silver;&amp;quot;&lt;br /&gt;
|&amp;lt;strong&amp;gt;Column&amp;lt;/strong&amp;gt;&lt;br /&gt;
|&amp;lt;strong&amp;gt;Type&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|uid&lt;br /&gt;
|INT&lt;br /&gt;
|-&lt;br /&gt;
|name&lt;br /&gt;
|VARCHAR(45)&lt;br /&gt;
|-&lt;br /&gt;
|email&lt;br /&gt;
|VARCHAR(45)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Now, put some records in there and move on to the Web setup.&lt;br /&gt;
===Web===&lt;br /&gt;
All you NEED to have is a PHP (or some other scripting solution that can handle JSON) program accepting connections.  Here is a sample that should be self-explanatory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
header('Content-type: application/json');  // this is the magic that sets responseJSON&lt;br /&gt;
&lt;br /&gt;
// Connecting, selecting database&lt;br /&gt;
$link = mysql_connect($dbhost, $dbuser, $dbpass)&lt;br /&gt;
    or die('Could not connect: ' . mysql_error());&lt;br /&gt;
mysql_select_db($dbname) or die('Could not select database');&lt;br /&gt;
&lt;br /&gt;
switch($_POST['op']) {&lt;br /&gt;
    case 'getAllRecords': {&lt;br /&gt;
        $table = $_POST['table'];&lt;br /&gt;
        $query = sprintf(&amp;quot;SELECT * FROM %s&amp;quot;, mysql_real_escape_string($table));&lt;br /&gt;
        // Performing SQL query&lt;br /&gt;
        $result = mysql_query($query) or die('Query failed: ' . mysql_error());&lt;br /&gt;
        $all_recs = array();&lt;br /&gt;
        while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {&lt;br /&gt;
            $all_recs[] = $line;&lt;br /&gt;
        }&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
echo json_encode($all_recs);&lt;br /&gt;
&lt;br /&gt;
// Free resultset&lt;br /&gt;
mysql_free_result($result);&lt;br /&gt;
&lt;br /&gt;
// Closing connection&lt;br /&gt;
mysql_close($link);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;Notes&amp;lt;/strong&amp;gt;:&lt;br /&gt;
*setting the &amp;quot;Content-type&amp;quot; header to &amp;quot;application/json&amp;quot; is required to get responseJSON set in the response object&lt;br /&gt;
*$dbhost, $dbuser, and $dbpass (obviously) need to be set by you, it is beyond the scope of this tutorial to discuss proper hiding of your connection information&lt;br /&gt;
*'op' and 'table' will be set as part of the query from the Ajax request and will be present in $_POST, this allows the code to be generic enough to respond with a dump for any table name you pass&lt;br /&gt;
*your code can be tested in a normal Web browser BUT you must remove/comment out the header line (unless you want to download the result, then leave it in) AND you must check $_REQUEST in the PHP instead of $_POST (don't forget to change back when it's time for emulator testing)&lt;br /&gt;
&lt;br /&gt;
===Mojo===&lt;br /&gt;
Everything is coming along nicely, now you just need to have some code that will create an Ajax request to your PHP page and, upon success, process responseJSON to do whatever you need to do.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
SomeAssistant.prototype.readRemoteDbTable = function(table) {&lt;br /&gt;
    var url = 'http://www.myserver.com/pathTo/my-awesome-script.php';&lt;br /&gt;
&lt;br /&gt;
    try {&lt;br /&gt;
        if(!table) {&lt;br /&gt;
            throw('readRemoteDbTable(): table name must be defined');&lt;br /&gt;
        }&lt;br /&gt;
        var request = new Ajax.Request(url,{&lt;br /&gt;
            method: 'post',&lt;br /&gt;
            parameters: {'op': 'getAllRecords', 'table': table},&lt;br /&gt;
            evalJSON: 'true',&lt;br /&gt;
            onSuccess: this.readRemoteDbTableSuccess.bind(this),&lt;br /&gt;
            onFailure: function(){&lt;br /&gt;
                //Stuff to do if the request fails, ie. display error&lt;br /&gt;
                Mojo.Log.error('Failed to get Ajax response');&lt;br /&gt;
            }&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
    catch(e) {&lt;br /&gt;
        Mojo.log.error(e);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
SomeAssistant.prototype.readRemoteDbTableSuccess = function(response) {&lt;br /&gt;
&lt;br /&gt;
    Mojo.log.info('Got Ajax response: ' + response.responseText);&lt;br /&gt;
    var json = response.responseJSON;&lt;br /&gt;
&lt;br /&gt;
    try {&lt;br /&gt;
        for(var field in json){&lt;br /&gt;
            Mojo.log.info('Got field: ' + field + ' with value: ' + json[field]);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    catch(e) {&lt;br /&gt;
        Mojo.log.error(e);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;Notes&amp;lt;/strong&amp;gt;:&lt;br /&gt;
*'method' is where you set the POST request&lt;br /&gt;
*'parameters' holds the pieces of the query string('op' and 'table')&lt;br /&gt;
*evalJSON is probably optional, but it makes me feel better to use it&lt;br /&gt;
Of course, now you need to call &amp;lt;strong&amp;gt;readRemoteDbTable('users')&amp;lt;/strong&amp;gt; in order to test this.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
readRemoteDbTable('users');&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Good Coding Practices==&lt;br /&gt;
Keep in mind that this example is just that: an example. You normally wouldn't want to create code in PHP that will return the entire contents of a table. Having said that, use this to expand upon the technique and gather the specific data you need. Remember to keep all data retrieval operations, massaging, etc on the server-side to prevent unauthorized access to your database. If you're storing user-specific information, you'll most likely want to create a method to login, return an authentication token and use that for all further operations when requesting data. This helps keep the requests secure and prevents unauthorized access from third parties.&lt;/div&gt;</summary>
		<author><name>Anno</name></author>
	</entry>
	<entry>
		<id>http://wiki.webos-internals.org/index.php?title=Talk:Tutorials_webOS_Getting_JSON_From_An_External_MySQL_Database&amp;diff=6765</id>
		<title>Talk:Tutorials webOS Getting JSON From An External MySQL Database</title>
		<link rel="alternate" type="text/html" href="http://wiki.webos-internals.org/index.php?title=Talk:Tutorials_webOS_Getting_JSON_From_An_External_MySQL_Database&amp;diff=6765"/>
		<updated>2009-10-31T03:47:29Z</updated>

		<summary type="html">&lt;p&gt;Anno: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I would '''''HIGHLY''''' suggest using this example as an example alone. You NEVER want to create a direct link to your database. Instead, you want to abstract it.&lt;br /&gt;
&lt;br /&gt;
Instead of accessing a table called users, you'd probably want to create a method for logging in, permissions, etc so no one can just view the contents of an entire table.&lt;br /&gt;
&lt;br /&gt;
In fact, I feel that this tutorial is quite negligent as it doesn't teach good practice at all.&lt;br /&gt;
&lt;br /&gt;
Remember, keep all logic that can retrieve data on the server ''only'' and never anywhere else. If you want users, you should &amp;quot;request&amp;quot; the users from the server and the server should determine what kind of data you're allowed to get.&lt;/div&gt;</summary>
		<author><name>Anno</name></author>
	</entry>
	<entry>
		<id>http://wiki.webos-internals.org/index.php?title=Talk:Tutorials_webOS_Getting_JSON_From_An_External_MySQL_Database&amp;diff=6764</id>
		<title>Talk:Tutorials webOS Getting JSON From An External MySQL Database</title>
		<link rel="alternate" type="text/html" href="http://wiki.webos-internals.org/index.php?title=Talk:Tutorials_webOS_Getting_JSON_From_An_External_MySQL_Database&amp;diff=6764"/>
		<updated>2009-10-31T03:45:40Z</updated>

		<summary type="html">&lt;p&gt;Anno: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I would '''''HIGHLY''''' suggest using this example as an example alone. You NEVER want to create a direct link to your database. Instead, you want to abstract it.&lt;br /&gt;
&lt;br /&gt;
Instead of accessing a table called users, you'd probably want to create a method for logging in, permissions, etc so no one can just view the contents of an entire table.&lt;br /&gt;
&lt;br /&gt;
In fact, I feel that this tutorial is quite negligent as it doesn't teach good practice at all.&lt;/div&gt;</summary>
		<author><name>Anno</name></author>
	</entry>
	<entry>
		<id>http://wiki.webos-internals.org/index.php?title=Talk:Tutorials_webOS_Getting_JSON_From_An_External_MySQL_Database&amp;diff=6763</id>
		<title>Talk:Tutorials webOS Getting JSON From An External MySQL Database</title>
		<link rel="alternate" type="text/html" href="http://wiki.webos-internals.org/index.php?title=Talk:Tutorials_webOS_Getting_JSON_From_An_External_MySQL_Database&amp;diff=6763"/>
		<updated>2009-10-31T03:45:24Z</updated>

		<summary type="html">&lt;p&gt;Anno: New page: I would '''''HIGHLY''''' suggest using this example as an example alone. You NEVER want to create a directly link to your database. Instead, you want to abstract it.  Instead of accessing ...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I would '''''HIGHLY''''' suggest using this example as an example alone. You NEVER want to create a directly link to your database. Instead, you want to abstract it.&lt;br /&gt;
&lt;br /&gt;
Instead of accessing a table called users, you'd probably want to create a method for logging in, permissions, etc so no one can just view the contents of an entire table.&lt;br /&gt;
&lt;br /&gt;
In fact, I feel that this tutorial is quite negligent as it doesn't teach good practice at all.&lt;/div&gt;</summary>
		<author><name>Anno</name></author>
	</entry>
</feed>