数组正确打印数据但查询只显示一半

时间:2016-02-24 14:20:11

标签: php mysql arrays

我在这里遇到一个非常奇怪的问题。我在php中的数组内部有数据。我想要数组必须查询数据库并获得结果的每个项目。它只向我展示了具体的结果而不是一切。

我的代码:

foreach($my as $k=> $v){
    //echo "Key: ". $k . " Value: " . $v . "<br/>";
    $sql2 = "SELECT column10 FROM `table` WHERE column1 = '$v' ";
    $res2 = mysql_command($sql2);
    echo $sql2 . "<br/>";
    $rowA = mysql_fetch_assoc($res2);
    //echo "<strong>Alternative: </strong>" . $v. "<strong> Auto Alternative: </strong>" . $rowA['column10'] . "<br/>";

}
echo '</table>';
echo "<pre>";
print_r($my);
echo "</pre>";

在浏览器中,如果我回显查询并且Key:和值是这样的结果:

SELECT column10 FROM `table` WHERE column1 = 'Villetta La Canoa' 
SELECT column10 FROM `table` WHERE column1 = ' Casa Immerso nel Verde' 
SELECT column10 FROM `table` WHERE column1 = ' La Rosetta' 
SELECT column10 FROM `table` WHERE column1 = 'Agriturismo La Nonna' 
SELECT column10 FROM `table` WHERE column1 = ' Villetta Cassiopeia' 
SELECT column10 FROM `table` WHERE column1 = ' La Rosetta' 
SELECT column10 FROM `table` WHERE column1 = 'Ca Gianca 2' 
SELECT column10 FROM `table` WHERE column1 = ' Villetta Teresa' 
SELECT column10 FROM `table` WHERE column1 = ' Appartamento Pinamare' 
SELECT column10 FROM `table` WHERE column1 = ' Casa del Principe' 
SELECT column10 FROM `table` WHERE column1 = 'Ca Gianca 2' 
SELECT column10 FROM `table` WHERE column1 = ' Villetta Teresa' 
SELECT column10 FROM `table` WHERE column1 = ' Appartamento Pinamare' 
SELECT column10 FROM `table` WHERE column1 = ' Casa del Principe' 
SELECT column10 FROM `table` WHERE column1 = 'Ca Gianca 2' 
SELECT column10 FROM `table` WHERE column1 = ' Villetta Teresa' 
SELECT column10 FROM `table` WHERE column1 = ' Appartamento Pinamare' 
SELECT column10 FROM `table` WHERE column1 = ' Casa del Principe' 

关键,价值是:

Key: 0 Value: Villetta La Canoa
Key: 1 Value: Casa Immerso nel Verde
Key: 2 Value: La Rosetta
Key: 3 Value: Agriturismo La Nonna
Key: 4 Value: Villetta Cassiopeia
Key: 5 Value: La Rosetta
Key: 6 Value: Ca Gianca 2
Key: 7 Value: Villetta Teresa
Key: 8 Value: Appartamento Pinamare
Key: 9 Value: Casa del Principe
Key: 10 Value: Ca Gianca 2
Key: 11 Value: Villetta Teresa
Key: 12 Value: Appartamento Pinamare
Key: 13 Value: Casa del Principe
Key: 14 Value: Ca Gianca 2
Key: 15 Value: Villetta Teresa
Key: 16 Value: Appartamento Pinamare
Key: 17 Value: Casa del Principe

我得到的是:

Alternative: Villetta La Canoa Auto Alternative: Villa Ronchi, Casa Ciserai, Villino Torretta, Casa Bianca
Alternative: Casa Immerso nel Verde Auto Alternative: 
Alternative: La Rosetta Auto Alternative: 
Alternative: Agriturismo La Nonna Auto Alternative: Agriturismo Antico Granaio, Casa Ciserai, Villa Ronchi, La Rosetta
Alternative: Villetta Cassiopeia Auto Alternative: 
Alternative: La Rosetta Auto Alternative: 
Alternative: Ca Gianca 2 Auto Alternative: Ca Gianca 1, La Vigna 2, Villetta Teresa
Alternative: Villetta Teresa Auto Alternative: 
Alternative: Appartamento Pinamare Auto Alternative: 
Alternative: Casa del Principe Auto Alternative: 
Alternative: Ca Gianca 2 Auto Alternative: Ca Gianca 1, La Vigna 2, Villetta Teresa
Alternative: Villetta Teresa Auto Alternative: 
Alternative: Appartamento Pinamare Auto Alternative: 
Alternative: Casa del Principe Auto Alternative: 
Alternative: Ca Gianca 2 Auto Alternative: Ca Gianca 1, La Vigna 2, Villetta Teresa
Alternative: Villetta Teresa Auto Alternative: 
Alternative: Appartamento Pinamare Auto Alternative: 
Alternative: Casa del Principe Auto Alternative: 

任何人都可以解释什么是错的吗?

2 个答案:

答案 0 :(得分:4)

从我的角度来看,您可能需要修剪$ v以确保字符串的开头和结尾没有空格。

foreach($my as $k=> $v){
   $v = trim($v);

另外你应该试试&#39; LIKE&#39;运算符而不是&#39; =&#39;。也许添加&#39;%&#39;到$ v。

$sql2 = "SELECT column10 FROM `table` WHERE column1 LIKE '%{$v}%'";

答案 1 :(得分:3)

mysql_command到底是什么?我确定这是一个错字,你的意思是mysql_query(唉,即便如此: - /)

你不会得到一些结果,因为像这样的一些字符串背后有额外的空间

SELECT column10 FROM `table` WHERE column1 = ' Casa Immerso nel Verde' 
                                              ^

您的样本数据证实了这一点。你没有其他选择的唯一结果是那些有额外空间的结果。