马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 简简单单 于 2017-2-22 11:09 编辑
以下实例中你可以看到 = 和 != 运算符是不起作用的: mysql> SELECT * FROM tcount_tbl WHERE runoob_count = NULL;Empty set (0.00 sec)mysql> SELECT * FROM tcount_tbl WHERE runoob_count != NULL;Empty set (0.01 sec)查找数据表中 runoob_count 列是否为 NULL,必须使用IS NULL和IS NOT NULL,如下实例: mysql> SELECT * FROM tcount_tbl -> WHERE runoob_count IS NULL;+-----------------+----------------+| runoob_author | runoob_count |+-----------------+----------------+| mahnaz | NULL || Jen | NULL |+-----------------+----------------+2 rows in set (0.00 sec)mysql> SELECT * from tcount_tbl -> WHERE runoob_count IS NOT NULL;+-----------------+----------------+| runoob_author | runoob_count |+-----------------+----------------+| mahran | 20 || Gill | 20 |+-----------------+----------------+2 rows in set (0.00 sec) 使用PHP脚本处理 NULL 值PHP脚本中你可以在 if...else 语句来处理变量是否为空,并生成相应的条件语句。 以下实例中PHP设置了$runoob_count变量,然后使用该变量与数据表中的 runoob_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($runoob_count )){ $sql = 'SELECT runoob_author, runoob_count FROM tcount_tbl WHERE runoob_count = $runoob_count';}else{ $sql = 'SELECT runoob_author, runoob_count FROM tcount_tbl WHERE runoob_count IS $runoob_count';}mysql_select_db('RUNOOB');$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['runoob_author']} <br> ". "Count: {$row['runoob_count']} <br> ". "--------------------------------<br>";} echo "Fetched data successfully\n";mysql_close($conn);?>
|