0

I am trying to use PHP to do queries for a Android App I am creating however I am having problems with the nested quotes that I have when calling my code. Let me show you what I am trying to do My php code is as followed:

<?php

$theQuery = $_POST['query'];

if(sizeOfArray == 0){
    $clause = "";
}

$con = mysql_connect("localhost" , "localuser" , "NOTMYPASSWORD");

if(!$con){
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("localuser_MyApp" , $con);

$result = mysql_query($theQuery);

while($row = mysql_fetch_assoc($result)){
    $output[] = $row;
}

print(json_encode($output));

mysql_close($con);


?>

This is the String literal that I am using in Java. I am passing this String to PHP through the POST method. However this query doesn't work because of the single quotes in the where clause.

"SELECT I.id , I.name, I.price 
FROM Item I 
INNER JOIN Item_Menu IM ON I.id = IM.itemid 
INNER JOIN Menu_Type MT ON MT.id= IM.menuid 
WHERE MT.name='entrees'";

I tried using the following but this also doesn't work. I get a warning about how the result returned was not a proper SQL result.

"SELECT I.id , I.name, I.price 
FROM Item I 
INNER JOIN Item_Menu IM ON I.id = IM.itemid 
INNER JOIN Menu_Type MT ON MT.id= IM.menuid 
WHERE MT.name=\"entrees\"";

If I use an id instead or remove the WHERE clause instead of using an actual String value then the query works fine.

My questions is how can I format this query correctly in java so I can pass it to php properly? More information can be provided if necessary but I didn't want to overload this question with blocks of code.

1 Answer 1

2

You can pass the query in with the double quotes (like you have above). Then in PHP, str_replace the double quotes with single quotes

$theQuery = str_replace("\"", "'", $_POST['query']);
Sign up to request clarification or add additional context in comments.

1 Comment

This is a great idea and helped me with my work around. For some reason replacing "\ didn't actually replace the "\ from the java so instead I surrounded entrees with ?!?entrees?!? and then did a replace all with ?!? as suggested and that worked. Thanks a bunch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.