[英]DIfferent MySQL behaviour from MySQL query from PHP and directly at PHPMyAdmin
I am using a PHP file to return a JSON array to an iOS app. 我正在使用PHP文件将JSON数组返回到iOS应用。 The table that is being consulted has only 32 records. 被查询的表只有32条记录。 If I execute the iOS app, it receives an empty array when calling the PHP file. 如果我执行iOS应用程序,则在调用PHP文件时它将收到一个空数组。 If I execute the PHP on the web browser, the result is also an empty array. 如果我在Web浏览器上执行PHP,则结果也是一个空数组。 If I run the query that is included in the PHP file directly in PHPMyAdmin, the query shows the correct result. 如果我直接在PHPMyAdmin中运行PHP文件中包含的查询,则该查询将显示正确的结果。
This is the PHP file: 这是PHP文件:
<?php
$host= 'localhost';
$db = 'app_mujer';
$uid = 'XXXXXXXXXXXX';//
$pwd = 'XXXXXXXXXXXX';
$link = mysql_connect($host,$uid,$pwd) or die("No se puede conectar ");
mysql_query("SET NAMES 'utf8'");
mysql_select_db($db) or die ("No se puede seleccionar la bbdd");
$id= urldecode($_GET['id']);
$arr = array();
$rs = mysql_query("SELECT * FROM tbcoordenadas where titulo='$id'");
while ($obj = mysql_fetch_assoc($rs)){
$arr[] = $obj['procedencia'];
}
echo json_encode($arr);
?>
I have detected that the problem occurs only when the URL parameter get by $id contains a '& character in it. 我已经检测到仅当$ id获取的URL参数中包含'&字符时,才会出现此问题。
If I run the following query directly at PHPMyAdmin, the result is the expected record: 如果直接在PHPMyAdmin上运行以下查询,则结果为预期记录:
SELECT *
FROM `tbcoordenadas`
WHERE `titulo` = 'D & R'
Any help is welcome. 欢迎任何帮助。
I have detected that the problem occurs only when the URL parameter get by $id contains a '& character in it. 我已经检测到仅当$ id获取的URL参数中包含'&字符时,才会出现此问题。
That's where the problem is. 那就是问题所在。
http://www.example.com/example.php?id=1&2&3
For this request, the value of $_GET['id'] will be 1
(and you would expect it to be 1&2&3
). 对于此请求,$ _GET ['id']的值将为1
(并且您希望它为1&2&3
)。 This is because the &
symbol is used to add another URL parameter. 这是因为&
符号用于添加另一个URL参数。
Since the value includes &
, you need to urlencode() the ID before sending it to the PHP script. 由于该值包含&
,因此您需要先将该ID urlencode()发送到PHP脚本。 As pointed out by @Jared Farrish, this should be done on every GET value (to prevent problems such as this one). 正如@Jared Farrish指出的那样,应在每个GET值上执行此操作(以防止发生此类问题)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.