我正在尝试在我的PHP文件中执行以下查询。
show create table test.account;
当我回应响应时,我只得到查询的第一个单词,即CREATE。 我用单引号包围整个查询但是当我回应响应时,我只得到第一个单引号到下一个单引号的响应(下一个单引号用于某个默认字符串),而不是整个查询。
以下是我的API接口代码 -
@GET("createQueries.php/")
Call<String> loadCreateQueries(@Query("tableName") String tableName);
我的PHP代码 -
<?php
include_once 'db_functions.php';
$db = new DB_Functions();
require_once 'config.php';
// connecting to mysql
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
if ( !$con){
echo "Database Connection Error";
}
// selecting database
$select = mysqli_select_db($con,DB_DATABASE);
if ( !$select){
echo "Database Selection Error";
}
$tableName = $_GET["tableName"];
$result = mysqli_query($con, "SHOW CREATE TABLE test.".$tableName);
if ($result != false) {
$create_query = "'{";
while ($row = mysqli_fetch_assoc($result)) {
$fields = array_values($row);
$create_query = $create_query.$fields[1];
}
$create_query = $create_query."}'";
echo $create_query;
} else {
echo "Table not in server";
}
?>
响应
{CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`amount` float DEFAULT NULL,
`interestRate` float DEFAULT NULL,
`totalInterestReceived` float DEFAULT
客户代码 -
public void createTable() {
if (tableNames.length > tableIndex) {
NetworkUtil.getAPI(getApplicationContext()).loadCreateQueries(tableNames[tableIndex]).enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
progressDialog.dismiss();
if (response.isSuccessful()) {
String query = response.body().getAsString();
Log.d("Create Query - ", query);
} else System.out.println(response.errorBody());
}
@Override
public void onFailure(Call<JsonElement> call, Throwable t) {
t.printStackTrace();
}
});
}
}
答案 0 :(得分:0)
我不知道它是否是最好的方法,但我用%27替换单引号,在回声之前,我用单引号包围它。
这是更新的代码 -
if ($result != false) {
$create_query = "";
while ($row = mysqli_fetch_assoc($result)) {
$fields = array_values($row);
$create_query = $create_query.$fields[1];
}
//$create_query = $create_query."";
$create_query = str_replace('\'', '%27', $create_query);
echo "'".$create_query."'";
} else {
echo "Table not in server";
}