Solved

mysql php if statement sql sentence

Posted on 2011-02-14
2
302 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.

785 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