473,473 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dyanamic Select Query

250 Contributor
In my web application I'm using php and mysql. I want to create sql query to search function. There are many search fields. Sometimes user select only one field and some times two and sometimes many. How can I write that kind of query. This is my query.Other options are make, color,model,price etc..

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM vehicles WHERE vehicleMake='$make' AND vehiclemodel='$model
  2.  
Sep 7 '11 #1
3 1731
Rabbit
12,516 Recognized Expert Moderator MVP
You can use PHP to build a dynamic string using if then else statements and then pass that to MySQL.
Sep 7 '11 #2
ghjk
250 Contributor
This is my code. When I select only vehicle type.It will get only one record. I couldn't understand where I goes wrong. Please help me..
Expand|Select|Wrap|Line Numbers
  1. if(isset($_GET['S'])==5){
  2.  
  3.     $make=$_GET['make'];
  4.     $model=$_GET['model'];
  5.     $type=$_GET['type'];
  6.     $fuel=$_GET['fuel'];
  7.     $color=$_GET['color'];
  8.  
  9.  
  10.     $query = "SELECT * FROM vehicles where";
  11.     if($make){
  12.     $query .= " vehicleMake = '$make'";
  13.     }
  14.     if($model!=" ")
  15.     {
  16.     $query .= " and vehicleModel = '$model'";
  17.     }
  18.     if($type!= " "){
  19.     $query .= " and vehicleType = '$type'";
  20.     }
  21. if($fuel!= " "){
  22.     $query .= " and vehicleFuel = '$fuel'";
  23.     }
  24.  
  25.     $query.=';';
  26.  
  27.  
  28. $result = mysql_query($query)or die(mysql_error());
  29.  
  30. echo "<table width=100% align=center>";
  31.     echo "&nbsp;&nbsp;<tr height=20 bgcolor=#C0C0C0><td align=center>Vehicle Make</td><td align=center>Vehicle Model</td><td>SELECT</td></tr>";
  32.  
  33.     while($row = mysql_fetch_array($result))
  34.     {
  35.         foreach( $row AS $key => $val ){
  36.         $$key = stripslashes( $val );
  37.         }
  38.  
  39.     echo "<tr class=normalText>";
  40.  
  41.     echo "<td> $vehicleMake</td>";
  42.     echo "<td> $vehicleModel</td>";
  43.  
  44.     echo  "<td><a href='javascript:void(0);' onclick=\"LookupVehicle('$vehicleType','$vehicleMake'); return false;\">Select</a></td>";
  45.             echo "</tr>";
  46.     }
  47.     echo "</table>";
  48.  
  49.     }
  50.  
Sep 9 '11 #3
Rabbit
12,516 Recognized Expert Moderator MVP
You should output the sql string it's trying to use to make sure it's correct.
Sep 9 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Greg Strong | last post by:
Hello All, Any reason why a select query that runs will not run as an update query? What I've done is created a select query that runs successfully. The query has several custom functions to...
9
by: MLH | last post by:
I have a table (tblCorrespondence) holding records with fields like , , , , , , , etc... About a dozen 's are defined and I often use queries to extract records of a given . That's pretty easy....
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
3
by: phonl | last post by:
I am a vb6 ADO developer looking at vb.net 2005 and ADO2.net. I used the vb.net 2005 data wizard to bind some controls to a database. Now I want to run a select query and have the bound controls...
5
by: parwal.sandeep | last post by:
Hello grp! i'm using INNODB tables which are using frequently . if i fire a SELECT query which fetch major part of table it usually take 10-20 seconds to complete. in mean time if any UPDATE...
1
by: Phil | last post by:
Is it possible to swap rows and columns in select query output so that each record's data is displayed in a column? I want to collect data each day and display it in a query with each day's date...
1
by: steveyell | last post by:
Hi there, I have a database with 90,000 property records and I need to split the database into 5 geographic areas. Those areas are defined by postcode districts, and a number of postcode...
3
by: RAG2007 | last post by:
I'm using the QueryDef and Execute method to update a record in my MySQL backend. Problem: When the Passthrough update query is defined using QueryDef, it becomes a select query, and I cannot use...
8
by: Trevor2007 | last post by:
I am trying to hard code the following select query into a select case statement ie (case1 <statment>, case 2 <statment>) but I getteing Compiler error: expected line number or label, or...
1
by: TZEM | last post by:
Hi - I'm new to Access and trying to create a complicated database that records info about system interfaces. I want to create a crosstab query that returns a field from the interface record, if a...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.