我們如何使用PHP腳本處理儲存在MySQL表中的NULL值?

WBOY
發布: 2023-09-07 18:18:02
轉載
923 人瀏覽過

我們如何使用PHP腳本處理儲存在MySQL表中的NULL值?

我們可以在 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 = &#39;localhost:3036&#39;;
   $dbuser = &#39;root&#39;;
   $dbpass = &#39;rootpassword&#39;;
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(! $conn ) {
      die(&#39;Could not connect: &#39; . mysql_error());
   }

   if( isset($tutorial_count )) {
      $sql = &#39;SELECT tutorial_author, tutorial_count FROM tcount_tbl
         WHERE tutorial_count = $tutorial_count&#39;;
   } else {
      $sql = &#39;SELECT tutorial_author, tutorial_count FROM tcount_tbl
         WHERE tutorial_count IS $tutorial_count&#39;;
   }

   mysql_select_db(&#39;TUTORIALS&#39;);
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die(&#39;Could not get data: &#39; . mysql_error());
   }

   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
      echo "Author:{$row[&#39;tutorial_author&#39;]} <br> ".
         "Count: {$row[&#39;tutorial_count&#39;]} <br> ".
         "--------------------------------<br>";
   }
   echo "Fetched data successfully</p><p>";
   mysql_close($conn);
?>
登入後複製

以上是我們如何使用PHP腳本處理儲存在MySQL表中的NULL值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!