views:

677

answers:

4

I'd like to query a MySQL database via a RESTful service with the middleware being PHP. I'd like the output to be JSON. I'm a beginner in those areas. Are there any frameworks or scripts that can do this without requiring you to be an expert? I don't have a problem stringing a few scripts together if they can work.

Or, if it can be done much simpler without using REST (i.e. query parameters), that's fine.

In the end, I want to have an iPhone app fetch this data and have it returned via JSON. No javascript will be involved.

A: 

Check out this project: http://phprestsql.sourceforge.net/

On this site you will find a RESTful interface (written in PHP) to a database (a MySQL database, but that's not important). Below you will find a interactive tutorial that will get you accessing, adding and deleting rows from our database via our Javascript powered REST browser.

James Kolpack
Yes - I actually sent you an email because it just errors out on me. "error on line 1 at column 1: Document is empty" in index.php is what it says. When I view source, I see my database tables are there. However, the service doesn't output JSON.
Ah yes - you could wrap the responses with an XML to JSON conversion - perhaps with http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/ . As for the error message, it'd be best to seek help with the project administrators or the project forms : http://sourceforge.net/projects/phprestsql/support
James Kolpack
A: 

The PHP shouldn't necessarily be your middleware in your situation, you'll have to build your "RESTful" service somehow, whether it be with PHP or any other language. You don't have to be an expert, but I think CakePHP has some of these capabilities.

Maybe explain in more detail what you want to do.

Update

Try this out: http://techno-geeks.org/2009/08/easy-json-with-cakephp-and-jquery/

Not only is CakePHP easy to install and use, it's also easy to extend.

Joseph Silvashy
Can't use JQuery. This is going to go onto an iPhone. I've updated the OP.
I love cakePHP but I wouldn't recommend it to a beginner. There is defiantly a learning curve involved.
+2  A: 

You can convert a MySQL result set to JSON easily: http://phpclasses.nlared.com/browse/package/3195.html

For a RESTful interface, basically any hosted PHP script can function as a REST interface for your application.

Cesar
I took a look at it. In the example.php, he has require("dbcon.php");. I'm not sure what goes in dbcon.php. He also writes the query in example.php, which isn't what I want. I'd like to use something like REST to build the query.
dbconf is most likely referring to the mysql_connect function (which the author put into a seperate file, but could be inline'd pretty easily. This one-liner should do the trick: @mysql_connect ($servername,$dbuser,$dbpassword); ... fill in the parameters.
r00fus
Perfect. I have it successfully querying the DB now. How do I build my queries via the URL (in a RESTful manner if possible)? I'm still using the hard coded SELECT in the example.php.
The basic idea is to build your query using the URL path, for example: myapp.com/houses/california/ This would yield all houses available for sale in California.You can do this manually by looking at $_SERVER['REQUEST_URI'] in any PHP script or let a framework such as Code Igniter handle this for your.
Cesar
A: 

I'm not sure what you mean here, but let me give it a shot anyhow, I think you want to have a web based service running on the iPhone as an app, and you want to take advantage of you app on the server side to process JSON that the iPhone app (client) will be sending to the service.

The issue with you question is that you must create the JSON objects with JavaScript, because that is well... what they are, JavaScript objects, so you may have to rule out the use of JavaScript.

You can try the iPhone SDK's UIWebView. This will allow you to use a web app much as you would in a browser but also take advantage of *some of the iPhone's API and local storage.

As for being able to "not be an expert" I don't think this is possible. It seems that for something to really become successful in this way and with an app of this complexity, you really must be an expert.

Joseph Silvashy