Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

mysql php if statement sql sentence

Posted on 2011-02-14
2
Medium Priority
?
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
karthika_cts earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

670 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