Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with query

Status
Not open for further replies.

fsphp

Technical User
Aug 30, 2001
3
IT
Hi there I've a problem with this query:
SELECT DISTINCT Hotel.Nome,Indirizzo, Categorie.Categoria FROM Hotel,
Servizi,Categorie, Hoteldetail WHERE Hoteldetail.IDHotel = Hotel.IDHotel AND Hoteldetail.IDServizio =
Servizi.IDServizio AND Hoteldetail.IDCategoria=Categorie.IDCategoria AND Servizi.IDServizio LIKE '3'
AND Categorie.IDCategoria LIKE '2'
this work fine but if I change IDServizio Like '$Servizio%' it doesn't work. The variable is given by php form.
thank you in advance

 
try
$myquery = "SELECT DISTINCT Hotel.Nome,Indirizzo, Categorie.Categoria FROM Hotel,
Servizi,Categorie, Hoteldetail WHERE Hoteldetail.IDHotel = Hotel.IDHotel AND Hoteldetail.IDServizio =
Servizi.IDServizio AND Hoteldetail.IDCategoria=Categorie.IDCategoria AND IDServizio LIKE '".$Servizio."%'
AND Categorie.IDCategoria LIKE '2'";
 
don't work in that way. it works only if the variable is %, other variable give no result.
I try to explain better:If I try to run query from phpmyadmin I get right result but if I try with php I get all matches. please help telling me how to correct script

searchform.php looking:

<html>
<body>
<br>
<form name=&quot;Services&quot; method=&quot;POST&quot; action=&quot;search.php&quot;>
<table width=&quot;400&quot; border=&quot;0&quot;>
<tr>
<td><input type=&quot;checkbox&quot; name=&quot;Services[]&quot; value=&quot;&quot;>TV</td>
<td><input type=&quot;checkbox&quot; name=&quot;Services[]&quot; value=&quot;&quot;>pool</td>
<td><input type=&quot;checkbox&quot; name=&quot;Services[]&quot; value=&quot;&quot;>minibar</tr>
<tr><td><input type=&quot;checkbox&quot; name=&quot;Services[]&quot; value=&quot;&quot;>Tennis</td>
<td><input type=&quot;checkbox&quot; name=&quot;Services[]&quot; value=&quot;&quot;>babysittig</td>
</tr></table>
<br><p>Hotel:
<input type=text name=Nome size=25 maxlength=25>
Category:
<select name=&quot;Category&quot;>
<option>one star</option>
<option>two stars</option>
<option>three stars</option>
<option>four starse</option>
</select>
</p>
<p>Address
<input type=text name=Indirizzo size=25 maxlength=25>
Locality;
<input type=text name=Locality; size=25 maxlength=25>
</p>
<input type=&quot;submit&quot; value=&quot;Submit&quot;>
</form>
</body>
</html>

and search.php looking:
<html>
<body>

<?php

mysql_connect (localhost, user, pass);

mysql_select_db (hotelsearch);

if ($Località == &quot;&quot;)
{$Località = '%';}

if ($Categoria == &quot;&quot;)
{$Categoria = '%';}

if ($Servizi == &quot;&quot;)
{$Servizi = '%';}

$result = mysql_query (&quot;SELECT DISTINCT Hotel.Name,Address, Category.Category FROM Hotel,
Services,Category, Hoteldetail WHERE Hoteldetail.IDHotel = Hotel.IDHotel AND Hoteldetail.IDServices =
Services.IDServices AND Hoteldetail.IDCategory=Category.IDCategory AND Servicies.IDServicies LIKE '$Service%'
AND Category.IDCategory LIKE '$Category%' &quot;);

if ($row = mysql_fetch_array($result)) {

do {
print $row[&quot;Name&quot;];
print (&quot; &quot;);
print $row[&quot;Category&quot;];
print (&quot; &quot;);
print $row[&quot;Locality&quot;];
print (&quot; &quot;);
print $row[&quot;Address&quot;];
print (&quot;<p>&quot;);
print $row[&quot;Services&quot;];
print (&quot; &quot;);
} while($row = mysql_fetch_array($result));

} else {print &quot;Sorry, no records were found!&quot;;}

?>

</body>
</html>

Database has those tables:
1)category (IDCategory,Category,pics)
2)Hotel (IDhotel,name,category,IDLocality,address,pic)
3)Hotel Detail (IDHoteldetail,IDhotel,IDservice,IDlocality,IDCategory)
4)Locality (IDlocality,locality)
5)Services (IDservice,service)

thank in advance for helping
 
hi
your main problem was not your script, but your html code
so i changed it a little bit
you are using 3 names for one variable - Servizi, Services, Service ???????
i was not looking at your mysql connect and querying code, i was too lazy to create those tables on my server :)

so here's the code:

form.php
---------------------

<html>
<body>
<br>
<form name=&quot;Services&quot; method=&quot;post&quot; action=&quot;search.php&quot;>
<table width=&quot;400&quot; border=&quot;0&quot;>
<tr>
<td><input type=&quot;checkbox&quot; name=&quot;TV&quot; value=&quot;&quot;>TV</td>
<td><input type=&quot;checkbox&quot; name=&quot;pool&quot; value=&quot;&quot;>pool</td>
<td><input type=&quot;checkbox&quot; name=&quot;minibar&quot; value=&quot;&quot;>minibar</tr>
<tr><td><input type=&quot;checkbox&quot; name=&quot;Tennis&quot; value=&quot;&quot;>Tennis</td>
<td><input type=&quot;checkbox&quot; name=&quot;babysittig&quot; value=&quot;&quot;>babysittig</td>
</tr></table>
<br><p>Hotel:
<input type=&quot;text&quot; name=&quot;Nome&quot; size=25 maxlength=25>
Category:
<select name=&quot;Category&quot;>
<option value=1>one star</option>
<option value=2>two stars</option>
<option value=3>three stars</option>
<option value=4>four starse</option>
</select>
</p>
<p>Address
<input type=&quot;text&quot; name=&quot;Indirizzo&quot; size=25 maxlength=25>
Locality;
<input type=&quot;text&quot; name=&quot;Locality&quot; size=25 maxlength=25>
</p>
<input type=&quot;submit&quot; value=&quot;Submit&quot;>
</form>
</body>
</html>

----------------------------

search.php
-------------------------
<html>
<body>

<?php

mysql_connect (localhost, user, pass);

mysql_select_db (hotelsearch);

if (isset($Locality) && ($Locality == '')) {
$QueryLocality = ' AND Locality.locality LIKE \'%\'';
}
else {
$QueryLocality = ' AND Locality.locality LIKE \'%$Locality%\'';
}

if (isset($Category) && ($Category != '')) $QueryCategory = ' AND Category.IdCategory = '.$Category;

$QueryServices = '';

if (isset($TV)) $QueryServices .= ' AND Services.service = \'TV\'';
if (isset($pool)) $QueryServices .= ' AND Services.service = \'pool\'';
if (isset($minibar)) $QueryServices .= ' AND Services.service = \'minibar\'';
if (isset($Tennis)) $QueryServices .= ' AND Services.service = \'Tennis\'';
if (isset($babysitting)) $QueryServices .= ' AND Services.service = \'babysitting\'';

$myquery = &quot;SELECT DISTINCT Hotel.Name,Address, Category.Category FROM Hotel,
Services,Category, Hoteldetail WHERE Hoteldetail.IDHotel = Hotel.IDHotel AND Hoteldetail.IDServices =
Services.IDServices AND Hoteldetail.IDCategory=Category.IDCategory &quot;.$QueryServices.&quot;&quot;.$QueryCategory;

$result = mysql_query ($myquery);

if ($row = mysql_fetch_array($result)) {

do {
print $row[&quot;Name&quot;];
print (&quot; &quot;);
print $row[&quot;Category&quot;];
print (&quot; &quot;);
print $row[&quot;Locality&quot;];
print (&quot; &quot;);
print $row[&quot;Address&quot;];
print (&quot;<p>&quot;);
print $row[&quot;Services&quot;];
print (&quot; &quot;);
} while($row = mysql_fetch_array($result));

} else {print &quot;Sorry, no records were found!&quot;;}

?>

</body>
</html>

---------------

hope this works
 
Piti I want to thank you for interesting in my problem. I've tried your way but something doesn't work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top