Jump to content

PHP from iPhone app


Niall_

Recommended Posts

Hi,

 

I am new trying to make a simple test iPhone app that reads data from a MySql server via php get method. As a test I used a 'film' database but soon realised that if the get method contains two words '%20' is added to the middle.

 

As a workaround, I tried:

if (strpos($_GET['film'], '%20') !==false)

$film = str_replace("%20", " ", $_GET['film'];

else

$_GET[film]=$film;

 

and then appending $film to the end of my sql query.

 

I have tried many variations of this but cannot get it to work! Any advice would be much appreciated.

 

Niall

Link to comment
Share on other sites

To my knowledge to must use this command:

getCommands()

 

Like so:

 

function getCommands()
  {
    $films = array();
    foreach( $this->_commands as $films )
    {
      $selected_film []= $selected_film['title'];
    }
    return $films;

 

? I am not fully knowledgeable on Iphone Apps so i may be wrong.

Link to comment
Share on other sites

Many thanks for the replies, having looked up urldecode, that option is exactly what I was trying to do.

 

However, it does not seem to work alongside the sql that I am using. The first line works as desired but the second with the urldecode does not:

 

$rs = mysql_query("SELECT id,title,cert,violence,drugs,sex,language,comments FROM films WHERE title='$_GET[film]'");

 

 

$rs = mysql_query("SELECT id,title,cert,violence,drugs,sex,language,comments FROM films WHERE title='urldecode($_GET[film])'");

 

Any suggestions? Thanks again for the help!

Link to comment
Share on other sites

You can't just toss a php function into a string like that; you'd need to run the data through the function first, or concatenate the function into the string.

 

So this:

$film = urldecode($_GET['film']);
$rs = mysql_query("SELECT id,title,cert,violence,drugs,sex,language,comments FROM films WHERE title='$film'");

 

Or this:

$rs = mysql_query("SELECT id,title,cert,violence,drugs,sex,language,comments FROM films WHERE title='" . urldecode($_GET[film]) . "'");

 

P.S. You also really, really, really need to be sanitizing any user-supplied data before allowing it into a query string.

Link to comment
Share on other sites

Thanks for quick reply Pikachu,

 

I had also tried your first method previously but no luck. Thought it may have been a syntax error before so tried every other possible variation of (), [], ", '.

 

The second method does not return any results either. For some reason it will only return results when submitted from:

 

$rs = mysql_query("SELECT id,title,cert,violence,drugs,sex,language,comments FROM films WHERE title='$_GET[film]'");

 

Thanks also for advice malicious code being passed from user. Have not taken this into account yet as only prototype for a project I'm doing on localhost.

Link to comment
Share on other sites

There is no html form. The app is calling the php script from the below objective-c method:

 

- (void)updateString {


self.string = searchtext.text;
label.text = self.string;

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8888/title.php?film=%@",searchtext.text]]; 
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
if (dict)
{
	rows = [dict objectForKey:@"films"];
	[rows retain];
}
NSLog(@"Array: %@",rows);
[jsonreturn release];
NSLog(@"result after rel: %@", searchtext.text);

}

 

Link to comment
Share on other sites

Thanks again for the help, still no joy with rawurldecode().

 

That should have been null array not nil array - from app console:

 

2011-01-15 22:22:18.349 json_test[73525:207] Array: (null)
2011-01-15 22:22:18.351 json_test[73525:207] result after rel: avatar 

Result from rawurldecode and amended php.

 

Whereas this is what it looks like for a single word title without any rawurldecode() console.jpg

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.