Solved

mysql php if statement sql sentence

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

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.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 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