Solved

mysql php if statement sql sentence

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

740 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