简体   繁体   English

带标点符号的SQL查询

[英]Sql query with punctuation marks

I have a webServer Between my SQL database and my Android app. 我的SQL数据库和Android应用程序之间有一个webServer。 On the database I want to put a sentence with question marks but when the webserver catches the query, the echo is null. 在数据库上,我想添加带问号的句子,但是当网络服务器捕获查询时,echo为null。 This only happen when I use question marks, dots or commas in the phrase. 仅当我在短语中使用问号,点或逗号时,才会发生这种情况。 The code of the webserver is: Web服务器的代码为:

<?php
$hostname_localhost ="*****";
$database_localhost ="*****";
$username_localhost ="*****";
$password_localhost ="*****";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);

$resultado = mysql_query('SELECT pregunta,respuesta,intentos,frase FROM Frases');
if (!$resultado) {
    die('Consulta no válida: ' . mysql_error());
}

while ($fila = mysql_fetch_assoc($resultado)) {
    $pregunta = $fila['pregunta'];
    $respuesta = $fila['respuesta'];
    $intentos = $fila['intentos'];
    $frase = $fila['frase'];

}

mysql_close($localhost);

$data = array('pregunta' => $pregunta, 'respuesta' => $respuesta, 'intentos' => $intentos, 'frase' => $frase); 
print (json_encode($data));

?>

You are declaring the variables ($pregunta, $respuesta, $intentos, $frase) locally to the while loop, once the while loop is completed those variables no longer exist. 您是在while循环中本地声明变量($ pregunta,$ respuesta,$ intentos,$ frase),一旦while循环完成,这些变量将不再存在。

Change: 更改:

while ($fila = mysql_fetch_assoc($resultado)) {
    $pregunta = $fila['pregunta'];
    $respuesta = $fila['respuesta'];
    $intentos = $fila['intentos'];
    $frase = $fila['frase'];

}

mysql_close($localhost);

$data = array('pregunta' => $pregunta, 'respuesta' => $respuesta, 'intentos' =>     $intentos, 'frase' => $frase); 
print (json_encode($data));

To: 至:

$data = array();

while ($fila = mysql_fetch_assoc($resultado)) {
    $pregunta = $fila['pregunta'];
    $respuesta = $fila['respuesta'];
    $intentos = $fila['intentos'];
    $frase = $fila['frase'];

    $data = array('pregunta' => $pregunta, 'respuesta' => $respuesta, 'intentos' =>     $intentos, 'frase' => $frase); 
}

mysql_close($localhost);


print (json_encode($data));

The data is then assigned to a variable outside the while loop and is thus retained, allowing you to output the json encoded array. 然后,将数据分配给while循环之外的变量,并因此将其保留,从而允许您输出json编码的数组。

In your java code, you can send your GET variables that way. 在Java代码中,您可以通过这种方式发送GET变量。 encoded to utf-8 编码为utf-8

String query = URLEncoder.encode("Do I need to write a question mark?", "utf-8");
 String url = "http://questionmark.com/search?q=" + query;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM