2014-08-30 105 views
0

基本上我的问题是,在某些情况下,php中的mysql会返回一个没有错误的空数组。Php mysqli没有给出任何结果

我从表'newproducts'获取所有产品,其名称与被调用的网址相同。

所以,当我打电话: http://domain.com:9090/db/product/3-Acetyl-5-bromopyridine

正被调用的查询(url_decode后):

SELECT * FROM `newproducts` WHERE `name` = '3-Acetyl-5-bromopyridine' LIMIT 1 

其正确返回的所有产品。

但如果我叫: http://domain.com:9090/db/product/%5B3aa,4a%28E%29,5b,6aa%5D-4-%5B4-%283-Chlorophenoxy%29-3-oxo-1-butenyl%5Dhexahydro-5-hydroxy-2H-cyclopenta%5Bb%5Dfuran-2-one 后,我做urldecode查询是这样(在浏览器中使用的var_dump):

SELECT * FROM `newproducts` WHERE `name` = '[3aa,4a(E),5b,6aa]-4-[4-(3-Chlorophenoxy)-3-oxo-1-butenyl]hexahydro-5-hydroxy-2H-cyclopenta[b]furan-2-one' LIMIT 1 

返回任何结果(空数组),没有错误。

另外,上面在phpmyadmin中的确切查询正确地返回结果。

最后,我使用下面的行之前试过我没有运气运行查询:提前

mysqli_set_charset($con, "utf8"); 
mysqli_real_escape_string($con, $name); 

谢谢!

查询的代码:

function getOneResFromDB($con, $text) { 
    mysqli_set_charset($con, "utf8"); 
    mysqli_real_escape_string($con, $text); 

    $query = "SELECT * FROM `newproducts` WHERE `name` = '" . $text . "' LIMIT 1"; 

    $result = mysqli_query($con, $query) or die(mysqli_error($con)); 

    $array = array(); 
    while ($row = mysqli_fetch_assoc($result)) { 
     $array[] = $row; 
    } 

    return $array; 
} 
+0

echo $查询上述请求 – 2014-08-30 12:50:17

+0

我已经使用var_dump和echo两个查询,我已经显示了上面的输出。 Echo和var_dump显示相同的结果。 – 2014-08-30 12:53:11

回答

0

我不得不使用html_entity_decode为好。结果看起来是一样的,但它甚至没有var_dump,因为有些字符是由Web浏览器自动转换的。 html_entity_decode解决了它。