ezSQL使用方法

获取结果集:


$users = $db->get_results("SELECT name, email FROM users");
 
foreach ( $users as $user )
{
  echo $user->name;
  echo $user->email;
}

获取一条数据:


$user = $db->get_row("SELECT name,email FROM users WHERE id = 2");
 
echo $user->name;
echo $user->email;

获取元组总数:


$var = $db->get_var("SELECT count(*) FROM users");
echo $var;

插入一条新数据:


$db->query("INSERT INTO users (id, name, email) VALUES (NULL,'justin','jv@foo.com')");

更新数据:


$db->query("UPDATE users SET name = 'Justin' WHERE id = 2)");

显示调试信息:


$db->debug();

获取某个字段的所有数据:


$names = $db->get_col("SELECT name,email FROM users",0)
 
foreach ( $names as $name )
{
    echo $name;
}

连接另一个数据库:


$db->select("my_database");

开启数据缓存:


	//开启缓存
	$db->use_disk_cache = true;
	$db->cache_queries = true;
	$db->cache_dir = './cache/';
	$db->cache_timeout = 24;

发表评论