-2

How do I check that the php script works? I'm using php -f filename.php email@address.com, but I get errors. The php script is intended to work with an Android app to retrieve data where the email matches a posted email address. Here is the php code:

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("db_name", $con);
$email= mysql_real_escape_string($_POST['email']);
$result=mysql_query("SELECT * FROM `TableName` WHERE email='$email' ");

if($result){ 
  $num_rows = mysql_num_rows($result);
  print(json_encode($num_rows));
}

  mysql_close($con);
?> 

Or am I not able to test this script from the command line, and instead should use an html file?

Now, for example if I replace the line: $email= "myaddress@nd.edu"; and then type php -f getdbstats.php I get the correct result.

sAguinaga
  • 638
  • 13
  • 31
  • I just learned of a way to test the php script: `http://ip.address/any_or_no_folder/your_php_script.php?email=email@address.edu`, then on the browser one should see the output of script. – sAguinaga Aug 20 '12 at 10:49

2 Answers2

1

Command line php is a little different. It does not use POST or GET global arrays to store command line parameters. You have to use global variable called $argv which is populated with parameters from the command line. So in you code, email address is stored in the $argv[1] element of the array. $argv[0] is the name of the script. Read more here: http://php.net/manual/en/reserved.variables.argv.php

kingdaemon
  • 1,586
  • 1
  • 11
  • 8
  • Right, so the php code is fine. When used from my android app, the problem was that since I went from 2.3 to 4, I had to do the networking stuff in another thread (not the ui thread). For now I've added this code to my class: `if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); }` which I got from [here](http://stackoverflow.com/questions/8706464/defaulthttpclient-to-androidhttpclient/8706961#8706961). – sAguinaga Jun 20 '12 at 12:37
  • But Lars of vogella.com states "StrictMode should only be used during development and not in your live application" [here](http://www.vogella.com/articles/AndroidPerformance/article.html#strictmode). So I will look more into that. If any of you have response to him, please share it. – sAguinaga Jun 20 '12 at 12:41
0

It works after the edit because the script is no longer expecting a POST. Why is it important that you run this as a command line script... beats me. If you want to just test if the script works you can write a simple form with one textfield and a submit button to see if the inputs are being received correctly.

If for some reason you still intend to run this on command line take a look at this SO post

Community
  • 1
  • 1
Orlymee
  • 2,349
  • 1
  • 22
  • 24