473,748 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic where clause

I am having a problem using a dynamic where clause. I have a feeling
that I am overlooking something very simple, although I can't seem to
figure it out.

The error i'm getting is: You have an error in your SQL syntax. Check
the manual that corresponds to your MySQL server version for the right
syntax to use near 'ORDER BY full_name ASC LIMIT 0,25'

Any help would be greatly appreciated.

here is my code:

$ssn = $_POST['ssn'];
$state = $_POST['state'];
$lastname = $_POST['lastname'];
$firstname = $_POST['firstname'];

$whereclause .= ($ssn)?" and ssn='$ssn'":"";
$whereclause .= ($state)?" and state='$state'" :"";
$whereclause .= ($firstname)?" and firstname like '%$firstname%'" :"";
$whereclause .= ($lastname)?" and lastname like '%$lastname%'": "";

$where = 'WHERE'.substr( $whereclause, 4);

// Define sql to list customers
$sql_customers = "SELECT customers.*,
date_format(cus tomers.create_d ate, '%m/%d/%Y') as create_date,
concat(customer s.lastname,', ',customers.fir stname) as
full_name,
products.descri ption,
lookupclientsta tus.status
FROM customers
INNER JOIN products ON
customers.produ ct_id=products. product_id
INNER JOIN lookupclientsta tus ON
customers.statu s_id=lookupclie ntstatus.status _id
$where
ORDER BY full_name ASC
LIMIT $from,$max_resu lts
";

Jul 17 '05 #1
7 3548
diroddi wrote:
$whereclause .= ($ssn)?" and ssn='$ssn'":"";
$whereclause .= ($state)?" and state='$state'" :"";
$whereclause .= ($firstname)?" and firstname like '%$firstname%'" :"";
$whereclause .= ($lastname)?" and lastname like '%$lastname%'": "";

$where = 'WHERE'.substr( $whereclause, 4);


You should try outputting your SQL statement to see what it looks like.
If you did, you would see something that the first term of your
WHERE clause begins with "and". (WHERE and ssn=.....) You need to keep
track of whether or not you have added a term to the WHERE clause and
only include the "and" for terms other than the first.

NM

--
convert uppercase WORDS to single keystrokes to reply
Jul 17 '05 #2
diroddi wrote:
I am having a problem using a dynamic where clause.
So you have an SQL question, not a PHP question.
$ssn = $_POST['ssn'];
$state = $_POST['state'];
$lastname = $_POST['lastname'];
$firstname = $_POST['firstname'];

$whereclause .= ($ssn)?" and ssn='$ssn'":"";
$whereclause .= ($state)?" and state='$state'" :"";
$whereclause .= ($firstname)?" and firstname like '%$firstname%'" :"";
$whereclause .= ($lastname)?" and lastname like '%$lastname%'": "";
Uhm... that's all you check? Major security issue here!
$where = 'WHERE'.substr( $whereclause, 4);
read the documentation on implode, that's probably a better way to get
your "and"'s.

Also, what if $whereclause is empty?
// Define sql to list customers
$sql_customers = "SELECT customers.*,
date_format(cus tomers.create_d ate, '%m/%d/%Y') as create_date,
concat(customer s.lastname,', ',customers.fir stname) as
full_name,
products.descri ption,
lookupclientsta tus.status
FROM customers
INNER JOIN products ON
customers.produ ct_id=products. product_id
INNER JOIN lookupclientsta tus ON
customers.statu s_id=lookupclie ntstatus.status _id
$where
ORDER BY full_name ASC
LIMIT $from,$max_resu lts
";


Do an echo $sql_customers and check this. Otherwise, feed this directly
to mysql (guessing you use that one) and see what line it reports.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #3

diroddi wrote (in part):
The error i'm getting is: You have an error in your SQL syntax. Check
the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY full_name ASC LIMIT 0,25'

Any help would be greatly appreciated.

here is my code:

$ssn = $_POST['ssn'];
$state = $_POST['state'];
$lastname = $_POST['lastname'];
$firstname = $_POST['firstname'];

$whereclause .= ($ssn)?" and ssn='$ssn'":"";
$whereclause .= ($state)?" and state='$state'" :"";
$whereclause .= ($firstname)?" and firstname like '%$firstname%'" :"";
$whereclause .= ($lastname)?" and lastname like '%$lastname%'": "";

$where = 'WHERE'.substr( $whereclause, 4);

// Define sql to list customers
$sql_customers = "SELECT customers.*,
date_format(cus tomers.create_d ate, '%m/%d/%Y') as create_date,
concat(customer s.lastname,', ',customers.fir stname) as
full_name,
products.descri ption,
lookupclientsta tus.status
FROM customers
INNER JOIN products ON
customers.produ ct_id=products. product_id
INNER JOIN lookupclientsta tus ON
customers.statu s_id=lookupclie ntstatus.status _id
$where
ORDER BY full_name ASC
LIMIT $from,$max_resu lts
";


Have you tried printing out your query before you execute it? SQL is
telling you that you have an error in your query. What does it look
like?

Ken

Jul 17 '05 #4
News Me wrote:
diroddi wrote:
$whereclause .= ($ssn)?" and ssn='$ssn'":"";
$whereclause .= ($state)?" and state='$state'" :"";
$whereclause .= ($firstname)?" and firstname like '%$firstname%'" :"";
$whereclause .= ($lastname)?" and lastname like '%$lastname%'": "";

$where = 'WHERE'.substr( $whereclause, 4);

You should try outputting your SQL statement to see what it looks like.
If you did, you would see something that the first term of your WHERE
clause begins with "and". (WHERE and ssn=.....) You need to keep track
of whether or not you have added a term to the WHERE clause and only
include the "and" for terms other than the first.

NM


Whoops! Missed the "substr". NEVER MIND!

NM

--
convert uppercase WORDS to single keystrokes to reply
Jul 17 '05 #5
News Me wrote:
diroddi wrote:
$whereclause .= ($ssn)?" and ssn='$ssn'":"";
$whereclause .= ($state)?" and state='$state'" :"";
$whereclause .= ($firstname)?" and firstname like '%$firstname%'" :"";
$whereclause .= ($lastname)?" and lastname like '%$lastname%'": "";

$where = 'WHERE'.substr( $whereclause, 4);

You should try outputting your SQL statement to see what it looks like.
If you did, you would see something that the first term of your WHERE
clause begins with "and". (WHERE and ssn=.....) You need to keep track
of whether or not you have added a term to the WHERE clause and only
include the "and" for terms other than the first.

NM


Even easier, do something like this: assuming that $ssn, $state,
$firstname and $slastname all have values:

$where_items[] = "ssn='$ssn' ";
$where_items[] = "state='$state' ";
$where_items[] = "firstname like '%$firstname%'" ;
$where_items[] = "lastname like '%$lastname%'";

$where = 'WHERE ' . join(' AND ', $where_items);

JP

--
Sorry, <de*****@cauce. org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #6
Thanks for the fast responses everybody.

Turns out that it was the empty $where. Hers's what I added to correct
it.

$tmp_where = 'WHERE '.substr($where clause, 4);

if($tmp_where== "WHERE ")
{
$where="";
}
else
{
where=$tmp_wher e;
}

I am a novice experimenting with different things. If there is a better
way (and I'm sure there is) to use variables
to build SQL where clauses I really want to know. Also, if anybody
knows of better place to post on MySQL I really need to know.

I'm off to read about cleaning my variables from harm, intentional or
otherwise. I'm going to start with addslashes() and stripslashes(). Any
other ideas?

Thanks again

Jul 17 '05 #7
diroddi wrote:
Turns out that it was the empty $where. Hers's what I added to correct
it.

$tmp_where = 'WHERE '.substr($where clause, 4);

if($tmp_where== "WHERE ")
{
$where="";
}
else
{
where=$tmp_wher e;
}


Ugly hack:

You know $whereclause is either empty or " and x1=y1[ and x2=y2[ ...]]";
if you do

'WHERE 1' . $whereclause

you get something like

'WHERE 1' or
'WHERE 1 and id=6 and cat=4'

so you can get rid of the $tmp_where and substr() call and do

$sql_customers = "SELECT ...
...
WHERE 1$whereclause
ORDER BY ...";
Please don't hit me! I said it was an ugly hack :-)

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
24641
by: Robert Scheer | last post by:
Hi. I have a stored procedure on a Oracle 8.1.6 database that generates a dynamic sql statement. This stored procedure has an output parameter that needs to return a count from a view. I can generate and run the sql successfuly, but when I try to return the count I get errors. I am showing the relevant part of the procedure, since the rest of concatenations are used to compose the sql. CREATE OR REPLACE PROCEDURE getScoreEntries (user...
4
4789
by: jrefactors | last post by:
I want to distinguish between static SQL, dynamic SQL, and embedded SQL, but couldn't find too much useful resources in the web. For example, if we put SQL statements (SELECT, INSERT, UPDATE, etc...) inside an application (e.g. Java application, VB application, etc...), do we consider those SQL statements as static SQL? or embedded SQL? How about dynamic SQL? any practical examples? Please advise. thanks!!
2
8853
by: klh | last post by:
We use DB2 Connect v 7.2 FP7 in Windows NT hitting a OS/390 DB2 v7.1 database. We have a Websphere (java) application that issues dynamic SQL. Most of the time when we issue dynamic SQL SELECT statements, like through a DB2 command window, the command will be processed using a package like SQLLF000 which uses an isolation level of Cursor Stability. However sometimes in the Websphere application when a dynamic SELECT statement is issued...
1
4325
by: Arijit Chatterjee | last post by:
Dear Faculties, I have a query on this statement.. =============================== CREATE PROCEDURE Check_Manage ( ) DYNAMIC RESULT SETS 1 ============================== I want to know the meaning of =============================== DYNAMIC RESULT SETS 1 ===============================
0
3516
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list boxes do not necessarily need to have a selection made to be used in the dynamic query. In essence the form can have selections made in all or none of its list boxes to form the dynamic query I am looking to get some feedback in reference to...
4
3804
by: Brian Shannon | last post by:
I have 3 combo boxes and two date text boxes on a .aspx page. The user can fill in any of the 5 controls or none to filter a datagrid. I was hoping someone could explain how to efficiently build the where clause of a sql string to send to SQL 2000 for a data set. Currenly I check each control with an IF statement to determine if something is filled in. If there is I begin building the where clause. Below is what I have done (and it...
13
17168
by: salad | last post by:
Operating in A97. I didn't receive much of a response conserning Pivot tables in Access. Pivot tables are nice, but a CrossTab will work for me too. Using a Pivot table, one is actually launching Excel for data viewing. I'd prefer the user stay in Access. Creating dynamic crosstab queries is pretty simple. The problem is that the column count may shrink or grow depending on the filter.
1
3748
by: john | last post by:
I'm trying to build a LINQ expression that will use a dynamic construction of a LIKE statement in the WHERE clause, it would look something like this in SQL: WHERE TaskGroup Like "*00*" OR TaskGroup Like "*20*" It would be many variations on the above.
0
5016
by: Jay Douglas | last post by:
Hello, I've found some posts on creating dynamic WHERE clauses in LINQ to SQL using predicate builders and extending lamda expressions. One issue with these posts is all the examples only use a single customer table. I'm need a way to dynamically create a where clause using multiple tables. None of the tables I'm using have references in the LINQ designer so the JOIN operator is required. If I were to hard type out the query in...
0
8995
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9558
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9253
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4608
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3316
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 we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.