Chinaunix首页 | 论坛 | 博客
  • 博客访问: 443093
  • 博文数量: 97
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 1091
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-17 17:05
个人简介

专注于大规模运维场景运维工具解决方案。欢迎有这方面兴趣的朋友跟我联系。

文章分类

全部博文(97)

文章存档

2014年(12)

2013年(25)

2012年(60)

我的朋友

分类: 系统运维

2012-08-01 21:48:36

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代码如下:

点击(此处)折叠或打开

  1. $conn = mysql_connect("localhost","root","321",true);
  2. mysql_select_db("test",$conn);
  3. mysql_query("set names utf8",$conn);
  4. mysql_query("set sql_mode='NO_AUTO_VALUE_ON_ZERO'",$conn);
  5. $query = mysql_query("select * from abc where id = 2",$conn);
  6. while($arr = mysql_fetch_array($query))
  7. {
  8.     print $arr["a"]."\t".is_null($arr["a"])."\t".empty($arr["a"])."\t".isset($arr["a"])."\n";
  9.     print $arr["b"]."\t".is_null($arr["b"])."\t".empty($arr["b"])."\t".isset($arr["b"])."\n";
  10.     print $arr["c"]."\t".is_null($arr["c"])."\t".empty($arr["c"])."\t".isset($arr["c"])."\n";
  11. }
输出:
1                       1
        1       1
                1       1
从记录来看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"];
}
不论是一条记录还是多条记录均可以这样处理的.


阅读(734) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~