- PHP 和 MySQL 教程
- PHP 和 MySQL - 主頁
- PHP 和 MySQL - 概述
- PHP 和 MySQL - 環境設定
- PHP 和 MySQL 示例
- PHP 和 MySQL - 連線資料庫
- PHP 和 MySQL - 建立資料庫
- PHP 和 MySQL - 刪除資料庫
- PHP 和 MySQL - 選擇資料庫
- PHP 和 MySQL - 建立表
- PHP 和 MySQL - 刪除表
- PHP 和 MySQL - 插入記錄
- PHP 和 MySQL - 選擇記錄
- PHP 和 MySQL - 更新記錄
- PHP 和 MySQL - 刪除記錄
- PHP 和 MySQL - Where 子句
- PHP 和 MySQL - Like 子句
- PHP 和 MySQL - 排序資料
- PHP 和 MySQL - 使用連線
- PHP 和 MySQL - 處理 NULL
- PHP 和 MySQL - 資料庫資訊
- PHP 和 MySQL 實用資源
- PHP 和 MySQL - 快速指南
- PHP 和 MySQL - 實用資源
- PHP 和 MySQL - 討論
PHP 和 MySQL - 處理 NULL 示例
你可以根據 NULL 值使用 if...else 條件來準備查詢。
以下示例從外部獲取 tutorial_count ,然後將它與表中可用的值進行比較。
示例
將以下示例複製並貼上為 mysql_example.php −
<html>
<head>
<title>Handling NULL</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root@123';
$dbname = 'TUTORIALS';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$tutorial_count = null;
if($mysqli->connect_errno ) {
printf("Connect failed: %s<br />", $mysqli->connect_error);
exit();
}
printf('Connected successfully.<br />');
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 NULL';
}
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
printf("Author: %s, Count: %d <br />",
$row["tutorial_author"],
$row["tutorial_count"]);
}
} else {
printf('No record found.<br />');
}
$mysqli->close();
?>
</body>
</html>
輸出
訪問部署在 apache Web 伺服器上的 mysql_example.php 並驗證輸出。
Connected successfully. No record found.
廣告