繁体   English   中英

带标点符号的SQL查询

[英]Sql query with punctuation marks

我的SQL数据库和Android应用程序之间有一个webServer。 在数据库上,我想添加带问号的句子,但是当网络服务器捕获查询时,echo为null。 仅当我在短语中使用问号,点或逗号时,才会发生这种情况。 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));

?>

您是在while循环中本地声明变量($ pregunta,$ respuesta,$ intentos,$ frase),一旦while循环完成,这些变量将不再存在。

更改:

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));

至:

$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));

然后,将数据分配给while循环之外的变量,并因此将其保留,从而允许您输出json编码的数组。

在Java代码中,您可以通过这种方式发送GET变量。 编码为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