1. empty(0) = ?
官方:
The following things are considered to be empty:
- "" (an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- $var; (a variable declared, but without a value)
2. MYSQL里面的NULL字段与emptystring字段映射到PHP里面是什么类型?
mysql的表结构:
| Table | Create Table
+-------+-----------------------------------------------
--------------------------------------------------------
| abc | CREATE TABLE `abc` (
`id` int(11) NOT NULL auto_increment,
`a` char(255) default NULL,
`b` char(255) default NULL,
`c` char(255) default '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 |
记录:
mysql> select * from abc;
+----+------+------+------+
| id | a | b | c |
+----+------+------+------+
| 2 | 1 | NULL | |
+----+------+------+------+
PHP代码如下:
- $conn = mysql_connect("localhost","root","321",true);
- mysql_select_db("test",$conn);
- mysql_query("set names utf8",$conn);
- mysql_query("set sql_mode='NO_AUTO_VALUE_ON_ZERO'",$conn);
- $query = mysql_query("select * from abc where id = 2",$conn);
- while($arr = mysql_fetch_array($query))
- {
- print $arr["a"]."\t".is_null($arr["a"])."\t".empty($arr["a"])."\t".isset($arr["a"])."\n";
- print $arr["b"]."\t".is_null($arr["b"])."\t".empty($arr["b"])."\t".isset($arr["b"])."\n";
- print $arr["c"]."\t".is_null($arr["c"])."\t".empty($arr["c"])."\t".isset($arr["c"])."\n";
- }
输出:
从记录来看b is NULL,所以PHP里面is_null()输出为TRUE。那empty肯定是TRUE;
c是空字符串,所以PHP里面的empty为TRUE其他的为FALSE。
总结:所以在导数据的时候如果遇到字段为is_null成立的话说明这个字段在MYSQL里面是null字段类型的可以不导过来的。或者是empty类型的数据除0以外哦!
3. mysql常见的一种遍历结果集
$query = mysql_query("select * from abc where id = 2",$conn);
while($arr = mysql_fetch_array($query)) {
print $arr["a"];
}
不论是一条记录还是多条记录均可以这样处理的.
阅读(760) | 评论(0) | 转发(0) |