PHP script returns the raw code rather than echoed variable in Android
I'm implementing an Android-MySQL interaction with PHP scripts.
The script works fine when I was running it on my laptop.
When I was trying to handle the return value from the PHP scripts (returned by echo) in my Android application, it turns out the whole raw PHP script was returned.
I was using Genymotion as the emulator and I did set the url as 10.0.3.2 in my code.
While I was trying to visit the PHP page using Genymotion (10.0.3.2/test.php), the browser load the raw script as well.
Although I think its more like a port/configuration problem, below are my PHP scrips and Android code snippet.
PHP:
<?php
$db_name = "demo";
$mysql_username = "root";
$mysql_password = "password";
$server_name = "127.0.0.1";
$sql_query = "select * from table1;";
// establish connection to mysql database
$con = mysqli_connect($server_name, $mysql_username, $mysql_password,
$db_name);
$result = mysqli_query($con, $sql_query);
$return_arr = array();
while ($row = mysqli_fetch_array($result))
{
$return_arr = array(
'id' => $row['id'],
'size' => $row['size'],
'material' => $row['material'],
'date' => $row['date']
);
}
header('Content-Type: application/json');
echo json_encode(array("server_response"=>$return_arr));
mysqli_close($con);
?>
Android:
System.out.println("nooo!");
StringRequest stringRequest = new StringRequest(Request.Method.POST, json_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queueVolley.add(stringRequest);
The question is how make the PHP script return the correct data in Android?
It was running on a MacBook Pro with Mojave OS.
Thank you!
php android
|
show 10 more comments
I'm implementing an Android-MySQL interaction with PHP scripts.
The script works fine when I was running it on my laptop.
When I was trying to handle the return value from the PHP scripts (returned by echo) in my Android application, it turns out the whole raw PHP script was returned.
I was using Genymotion as the emulator and I did set the url as 10.0.3.2 in my code.
While I was trying to visit the PHP page using Genymotion (10.0.3.2/test.php), the browser load the raw script as well.
Although I think its more like a port/configuration problem, below are my PHP scrips and Android code snippet.
PHP:
<?php
$db_name = "demo";
$mysql_username = "root";
$mysql_password = "password";
$server_name = "127.0.0.1";
$sql_query = "select * from table1;";
// establish connection to mysql database
$con = mysqli_connect($server_name, $mysql_username, $mysql_password,
$db_name);
$result = mysqli_query($con, $sql_query);
$return_arr = array();
while ($row = mysqli_fetch_array($result))
{
$return_arr = array(
'id' => $row['id'],
'size' => $row['size'],
'material' => $row['material'],
'date' => $row['date']
);
}
header('Content-Type: application/json');
echo json_encode(array("server_response"=>$return_arr));
mysqli_close($con);
?>
Android:
System.out.println("nooo!");
StringRequest stringRequest = new StringRequest(Request.Method.POST, json_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queueVolley.add(stringRequest);
The question is how make the PHP script return the correct data in Android?
It was running on a MacBook Pro with Mojave OS.
Thank you!
php android
1
Possible duplicate of PHP code is not being executed, instead code shows on the page
– miken32
Nov 21 '18 at 4:34
@miken32, hi Mike, my PHP script actually works on my own laptop browser, it's just not working on the Android code and emulator. I checked the accepted answer in that thread and it was not quite my case.
– Yezhen Wang
Nov 21 '18 at 4:40
If your server is returning the PHP code, it's not configured properly, nothing to do with the client side.
– miken32
Nov 21 '18 at 4:42
@miken32 but it is not returning PHP code and works fine when I'm visiting it on my laptop.
– Yezhen Wang
Nov 21 '18 at 4:44
What URL are you accessing?
– miken32
Nov 21 '18 at 4:45
|
show 10 more comments
I'm implementing an Android-MySQL interaction with PHP scripts.
The script works fine when I was running it on my laptop.
When I was trying to handle the return value from the PHP scripts (returned by echo) in my Android application, it turns out the whole raw PHP script was returned.
I was using Genymotion as the emulator and I did set the url as 10.0.3.2 in my code.
While I was trying to visit the PHP page using Genymotion (10.0.3.2/test.php), the browser load the raw script as well.
Although I think its more like a port/configuration problem, below are my PHP scrips and Android code snippet.
PHP:
<?php
$db_name = "demo";
$mysql_username = "root";
$mysql_password = "password";
$server_name = "127.0.0.1";
$sql_query = "select * from table1;";
// establish connection to mysql database
$con = mysqli_connect($server_name, $mysql_username, $mysql_password,
$db_name);
$result = mysqli_query($con, $sql_query);
$return_arr = array();
while ($row = mysqli_fetch_array($result))
{
$return_arr = array(
'id' => $row['id'],
'size' => $row['size'],
'material' => $row['material'],
'date' => $row['date']
);
}
header('Content-Type: application/json');
echo json_encode(array("server_response"=>$return_arr));
mysqli_close($con);
?>
Android:
System.out.println("nooo!");
StringRequest stringRequest = new StringRequest(Request.Method.POST, json_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queueVolley.add(stringRequest);
The question is how make the PHP script return the correct data in Android?
It was running on a MacBook Pro with Mojave OS.
Thank you!
php android
I'm implementing an Android-MySQL interaction with PHP scripts.
The script works fine when I was running it on my laptop.
When I was trying to handle the return value from the PHP scripts (returned by echo) in my Android application, it turns out the whole raw PHP script was returned.
I was using Genymotion as the emulator and I did set the url as 10.0.3.2 in my code.
While I was trying to visit the PHP page using Genymotion (10.0.3.2/test.php), the browser load the raw script as well.
Although I think its more like a port/configuration problem, below are my PHP scrips and Android code snippet.
PHP:
<?php
$db_name = "demo";
$mysql_username = "root";
$mysql_password = "password";
$server_name = "127.0.0.1";
$sql_query = "select * from table1;";
// establish connection to mysql database
$con = mysqli_connect($server_name, $mysql_username, $mysql_password,
$db_name);
$result = mysqli_query($con, $sql_query);
$return_arr = array();
while ($row = mysqli_fetch_array($result))
{
$return_arr = array(
'id' => $row['id'],
'size' => $row['size'],
'material' => $row['material'],
'date' => $row['date']
);
}
header('Content-Type: application/json');
echo json_encode(array("server_response"=>$return_arr));
mysqli_close($con);
?>
Android:
System.out.println("nooo!");
StringRequest stringRequest = new StringRequest(Request.Method.POST, json_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queueVolley.add(stringRequest);
The question is how make the PHP script return the correct data in Android?
It was running on a MacBook Pro with Mojave OS.
Thank you!
php android
php android
edited Nov 21 '18 at 4:43
Yezhen Wang
asked Nov 21 '18 at 4:19
Yezhen WangYezhen Wang
32
32
1
Possible duplicate of PHP code is not being executed, instead code shows on the page
– miken32
Nov 21 '18 at 4:34
@miken32, hi Mike, my PHP script actually works on my own laptop browser, it's just not working on the Android code and emulator. I checked the accepted answer in that thread and it was not quite my case.
– Yezhen Wang
Nov 21 '18 at 4:40
If your server is returning the PHP code, it's not configured properly, nothing to do with the client side.
– miken32
Nov 21 '18 at 4:42
@miken32 but it is not returning PHP code and works fine when I'm visiting it on my laptop.
– Yezhen Wang
Nov 21 '18 at 4:44
What URL are you accessing?
– miken32
Nov 21 '18 at 4:45
|
show 10 more comments
1
Possible duplicate of PHP code is not being executed, instead code shows on the page
– miken32
Nov 21 '18 at 4:34
@miken32, hi Mike, my PHP script actually works on my own laptop browser, it's just not working on the Android code and emulator. I checked the accepted answer in that thread and it was not quite my case.
– Yezhen Wang
Nov 21 '18 at 4:40
If your server is returning the PHP code, it's not configured properly, nothing to do with the client side.
– miken32
Nov 21 '18 at 4:42
@miken32 but it is not returning PHP code and works fine when I'm visiting it on my laptop.
– Yezhen Wang
Nov 21 '18 at 4:44
What URL are you accessing?
– miken32
Nov 21 '18 at 4:45
1
1
Possible duplicate of PHP code is not being executed, instead code shows on the page
– miken32
Nov 21 '18 at 4:34
Possible duplicate of PHP code is not being executed, instead code shows on the page
– miken32
Nov 21 '18 at 4:34
@miken32, hi Mike, my PHP script actually works on my own laptop browser, it's just not working on the Android code and emulator. I checked the accepted answer in that thread and it was not quite my case.
– Yezhen Wang
Nov 21 '18 at 4:40
@miken32, hi Mike, my PHP script actually works on my own laptop browser, it's just not working on the Android code and emulator. I checked the accepted answer in that thread and it was not quite my case.
– Yezhen Wang
Nov 21 '18 at 4:40
If your server is returning the PHP code, it's not configured properly, nothing to do with the client side.
– miken32
Nov 21 '18 at 4:42
If your server is returning the PHP code, it's not configured properly, nothing to do with the client side.
– miken32
Nov 21 '18 at 4:42
@miken32 but it is not returning PHP code and works fine when I'm visiting it on my laptop.
– Yezhen Wang
Nov 21 '18 at 4:44
@miken32 but it is not returning PHP code and works fine when I'm visiting it on my laptop.
– Yezhen Wang
Nov 21 '18 at 4:44
What URL are you accessing?
– miken32
Nov 21 '18 at 4:45
What URL are you accessing?
– miken32
Nov 21 '18 at 4:45
|
show 10 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405197%2fphp-script-returns-the-raw-code-rather-than-echoed-variable-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405197%2fphp-script-returns-the-raw-code-rather-than-echoed-variable-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Possible duplicate of PHP code is not being executed, instead code shows on the page
– miken32
Nov 21 '18 at 4:34
@miken32, hi Mike, my PHP script actually works on my own laptop browser, it's just not working on the Android code and emulator. I checked the accepted answer in that thread and it was not quite my case.
– Yezhen Wang
Nov 21 '18 at 4:40
If your server is returning the PHP code, it's not configured properly, nothing to do with the client side.
– miken32
Nov 21 '18 at 4:42
@miken32 but it is not returning PHP code and works fine when I'm visiting it on my laptop.
– Yezhen Wang
Nov 21 '18 at 4:44
What URL are you accessing?
– miken32
Nov 21 '18 at 4:45