Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

จัดการค่า NULL ใน MySQL ด้วย PHP Script

การจัดการ NULL Value ใน PHP

ใช้เงื่อนไข if...else ใน PHP สร้าง Query แยกสำหรับค่า NULL

ตัวอย่าง: ตาราง tcount_tbl

mysql> SELECT * FROM tcount_tbl;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran          |             20 |
| mahnaz          |           NULL |
| Jen             |           NULL |
| Gill            |             20 |
+-----------------+----------------+

PHP Script

<?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);
?>

สังเกต: ค้นหา NULL ต้องใช้ IS NULL ไม่ใช่ = NULL โค้ดตัวอย่างมีข้อบกพร่องควรแก้เป็น IS NULL / IS NOT NULL