Reputation: 380
I am having a very very strange issue here. I am having data inside of an array in php. I want for each item that the array has to query the database and get results back. It only shows me specific results and not everything.
My code:
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>";
In the browser the result if i echo the query and Key: and value is this:
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'
The Key,Value is :
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
and what i get is:
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:
Can anyone explain what is wrong?
Upvotes: 2
Views: 127
Reputation: 1892
From my point of view you may have to trim $v to be sure there are no white spaces at the start and end of string.
foreach($my as $k=> $v){
$v = trim($v);
Also you should try the 'LIKE' operator instead of '='. Perhaps add '%' to $v.
$sql2 = "SELECT column10 FROM `table` WHERE column1 LIKE '%{$v}%'";
Upvotes: 4
Reputation: 46900
What on earth is mysql_command
? I'm sure its a typo and you meant mysql_query
(Alas even for that :-/)
And you dont get results for some because of an extra space behind some strings like this one
SELECT column10 FROM `table` WHERE column1 = ' Casa Immerso nel Verde'
^
Your sample data confirms this. The only results you have no alternatives for are the ones that have that extra space.
Upvotes: 3