I’ve looked around on here quite a bit for an answer to my problem but I can’t seem to find anything that helps. I’m building an Android application that needs to access student classes through a php script on the server. Each class is made up of a subject, subject number, and section number stored in a mysql database. I send the subject and subject number separated by a ‘\n’ character (“subject\nnumber”) to the php script so that it can find and return all sections under that course. Here’s the code my script runs:
$course = explode('\n', $_REQUEST['course']);
$sections=mysql_query("select section from class where subj = '".$course[0]."' and number = '".$course[1]."' order by section");
while($e=mysql_fetch_assoc($sections))
$output[]=$e;
print(json_encode($output));
When I pass in the subject and number through my browser with something like
getSections.php?course=CS\n101
It properly formats it into a JSON string and it works perfectly. But when I try to use it with my app, I get an undefined offset: 1 error at line 2. I’ve wrapped the input string in other characters to make sure that it was sending the same thing as what I try in my browser and it is. It’s really boggling my mind why it’s not working the way it should.
It’s also saying that $output is undefined but I’m pretty sure that’s because the while loop is failing.
Any help would be greatly appreciated.
You need to use double quotes for the explode \n ~
explode("\n", $_REQUEST['course']);Escape characters like newline (\n) only work in double quotes:
echo '\n'prints \nwhereas
echo "\n"will print a new line