PHP-তে if...else কন্ডিশন ব্যবহার করে NULL ভ্যালু ভিত্তিক কোয়েরি প্রস্তুত করা যায়।
উদাহরণ
tcount_tbl টেবিল:
mysql> SELECT * FROM tcount_tbl;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran | 20 |
| mahnaz | NULL |
| Jen | NULL |
| Gill | 20 |
+-----------------+----------------+
PHP স্ক্রিপ্ট যা বাহ্যিক $tutorial_count ভ্যালু নেয় এবং NULL হ্যান্ডলিং করে:
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
if(isset($tutorial_count)) {
$sql = 'SELECT tutorial_author, tutorial_count FROM tcount_tbl
WHERE tutorial_count = $tutorial_count';
} else {
$sql = 'SELECT tutorial_author, tutorial_count FROM tcount_tbl
WHERE tutorial_count IS $tutorial_count';
}
mysql_select_db('TUTORIALS');
$retval = mysql_query($sql, $conn);
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Author:{$row['tutorial_author']} <br> ".
"Count: {$row['tutorial_count']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
নোট: PHP-তে IS NULL ব্যবহার করা উচিত, IS $variable নয়। উপরের উদাহরণে লজিক্যাল ভুল আছে। সঠিক কোডে IS NULL বা = NULL (যদি ভ্যারিয়েবল NULL হয়) ব্যবহার করতে হবে।