Solved

mysql php if statement sql sentence

Posted on 2011-02-14
2
301 Views
Last Modified: 2012-05-11
Dear Experts,

would it be correct to write it like this?

      if (!empty($name))
      {
      $sqlsentence. = " and Name like '" . $name . "%'"
      }
      
      if (!empty($gender))
      {
      $sqlsentence. = " and Gender like '" . $gender . "%'"
      }
      
      if (!empty($gender))
      {
      $sqlsentence. = " and City like '" . $city . "%'"
      }
      
$sqlsentence= "SELECT * FROM Mytable where 1=1";      

$mysqlsentence= "SELECT * FROM Mytable where 1=1" . $sqlsentence . ";
0
Comment
Question by:Braveheartli
2 Comments
 
LVL 5

Accepted Solution

by:
karthika_cts earned 500 total points
ID: 34886197
No, this would replace the conditions stored in the varaible $sqlsentence with the select query.

It should be like

      if (!empty($name))
      {
      $sqlsentence1 = " and Name like '" . $name . "%'"
      }
     
      if (!empty($gender))
      {
      $sqlsentence1 = " and Gender like '" . $gender . "%'"
      }
     
      if (!empty($gender))
      {
      $sqlsentence1 = " and City like '" . $city . "%'"
      }
     
$sqlsentence= "SELECT * FROM Mytable where 1=1";      

$mysqlsentence= "SELECT * FROM Mytable where 1=1" . $sqlsentence1 ";

Please try this n let me know
0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 34886209
I got that thank you
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now