Solved

mysql if sentence

Posted on 2011-02-14
3
327 Views
Last Modified: 2012-05-11
Dear Experts,
is there a way to merge this two sql statement?

              if (!empty($company))
      {
      $sqlsentence. = " and Company1 like '" . $company . "%'"
      }
        
                      if (!empty($company))
      {
      $sqlsentence. = " and Company2 like '" . $company . "%'"
      }
      
$mysqlsentence= "SELECT * FROM Mytable where 1=1" . $sqlsentence ";
0
Comment
Question by:Braveheartli
  • 2
3 Comments
 
LVL 1

Author Comment

by:Braveheartli
ID: 34886583
would it be correct to write it like this?

$sqlsentence. = " and Company1 like '" . $company . "%' and Company2 like '" . $company . "%'"
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 34886864
Yes.
if (!empty($company)){
 $sqlsentence. = " and Company1 like '" . $company . "%' and Company2 like '" . $company . "%'";
}
0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 34887012
thank you very much
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

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…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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 look for a specific file type in a local or remote server directory using PHP.

747 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

12 Experts available now in Live!

Get 1:1 Help Now