我們可以在 PHP 腳本中使用 if...else 條件來準備基於 NULL 值的查詢。為了說明這一點,我們使用以下範例-
在此範例中,我們使用名為 'tcount_tbl' 的表,其中包含以下資料-
mysql> SELECT * from tcount_tbl; +-----------------+----------------+ | tutorial_author | tutorial_count | +-----------------+----------------+ | mahran | 20 | | mahnaz | NULL | | Jen | NULL | | Gill | 20 | +-----------------+----------------+ 4 rows in set (0.00 sec)
現在,以下是一個PHP腳本,它從外部取得 'tutorial_count'的值,並將其與欄位中可用的值進行比較。
<?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</p><p>"; mysql_close($conn); ?>
以上是我們如何使用PHP腳本處理儲存在MySQL表中的NULL值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!