The usage of if and else in MYSQL and PHP

php mysql - mysql if else - php mysql tutorial

how to use if and else in php and mysql with simple way



The usage of if and else in MYSQL and PHP

Hallo friends of Tatamedia, web developer in Surabaya and Bali.
Now we will learn about the usage of if in mysql, so the query can be shortened.

Generally, if in mysql has a form as follows:

IF(condition, value_if_true, value_if_false)

where:
condition is the value to test.
value_if_true is the value to return if condition is TRUE.
value_if_false is the value to return if condition is FALSE.

Read: Restricted input type for number only by using HTML5

For example, we have a table, namely product which has two columns, namely nama and harga.
We have data in the table as follows:

nama | harga
Product A | 1000
Product B | 1500
Product C | 2000
Product D | 5000
Product E | 10000

Further, we will use if in mysql to show product with harga under 2000:

<?php
$result=$db->doQuery("SELECT nama,IF(harga < ?, "Promo", "Normal") as promo from product", Array(2000));
?>

The result is as follows:

nama | promo
Product A | Promo
Product B | Promo
Product C | Normal
Product D | Normal
Product E | Normal

By doing this, in PHP script we just call the value with command: $row['promo'].
Ofcourse we can use if and else in PHP language like follows:

<?php
if($row['harga'] < 2000) { echo 'Promo'; } else { echo 'Normal'; }
?>

But using that way is not effisient than using if in mysql.

Read: Restricted input type for number only by using HTML5

Read: Looking for birthday with PHP and MySQL

Can we use if in mysql for complicated query? Sure, we can.
You can make any variations in condition field as well as in value_if_true and value_if_false.
The example is as follows:

<?php
$result=$db->doQuery("SELECT nama,IF(nama=? , CONCAT(nama,' - Best Seller'), nama) as newname from product", Array('Product A'));
?>

CONCAT is Mysql's command to add several strings together.

The result is as follows:

nama | newname
Product A | Product A - Best Seller
Product B | Product B
Product C | Product C
Product D | Product D
Product E | Product E

It is easy, right? You can use with your need.
I hope you enjoy the tutorial of web developer in surabaya about the usage of if and else in PHP and Mysql.

Good luck.



Related Article

Web Design Services - Best Webdesign - Best SEO Services

Checking Password Strength with pStrength jQuery Plugin

When we make a web design, we often make registration form for the visitor. One of the field is password. One thing we need is password strength to ensure the better security for login access.. To fulfill that we can use pStrength jQuery Plugin.We j ...


Restricted input type for number only by using HTML5

How to make the input type to be valid for number only? We do not need to use javascript, because we can do that easily with HTML5. Let say we want to make an input type for temperature with the following rule: minimum 30o and maximum 50o, but allow ...


Looking for birthday with PHP and MySQL

We often experience difficulty to find the birthday between two dates inside MySQL database. It is caused by the storage we use in MySQL, namely brith date format like Y-m-d, for example 2000-12-21. The problem occurs when we try to find the birthda ...


We create RESPONSIVE WEBSITE

Responsive web is special program code, so the website can be read in all devices. It is very suitable for better page rank in search engine like google

graphic design in indonesia

Website Design in Indonesia

Graphic Design in Indonesia

Contact

+628113638531 / +628113865464

info@tatamedia.com

Webdesigner in Indonesia

Videographer in Bali

Office

Tatamedia Solusindo Surabaya

Dekkophotography Bali

Fine Art Photography in Bali

Web Design

Support

Send your request to

info@tatamedia.com