ezsql使用
文章描述:
ezsql一些常规操作使用
查询数据
get_results()
从数据库中选择多个记录并将其打印出来..
$users = $db->get_results("SELECT * FROM ld_users");
$users = $db->get_results("SELECT * FROM ".$db_name->table('users'));
foreach($users as $user)
{
//使用对象语法访问数据
echo $user->name;
echo $user->email;
}
get_row()
从数据库中获取一行并将其打印出来..
$user = $db->get_row("SELECT name,email FROM ".$db_name->table('users')." WHERE id = 3");
echo $user->name;
echo $user->email;
get_var()
从数据库中获取一个变量并将其打印出来..
$var = $db->get_var("SELECT count(*) FROM ".$db_name->table('users'));
echo $var;
执行SQL语句
insert
执行SQL语句,在query里面添加SQL语句,把数据插入数据库。
$sex = rand(1,2);
$age = rand(1,100);
$db->query("INSERT INTO ".$db_name->table('users')."(name,email,sex,age)VALUES('justin','jv@foo.com','$sex','$age')");
update
执行SQL语句,在query里面添加SQL语句,修改数据库内容。
//更新数据库
$db->query("UPDATE ".$db_name->table('users')." SET name ='小王' WHERE id = 2");
array_a
查询以数组方式
$users = $db->get_results("SELECT * FROM ".$db_name->table('users'),ARRAY_A);
$db->vardump($users);
get_col()
获取“一列”(基于列索引)并将其打印出来..
$names = $db->get_col("SELECT name,email FROM ".$db_name->table('users'),0);
foreach($names as $name)
{
echo $name;
}
//与上面相同'但更快'
//foreach($db->get_col("SELECT name,email FROM ".$db_name->table('users'),0)as $name)
{
echo $name;
}
查询判断是否有数据
$users = $db->get_results("SELECT * FROM ".$db_name->table('users'),ARRAY_N);
$db->vardump($users);
if($users = $db->get_results("SELECT * FROM ".$db_name->table('users'),ARRAY_A)){
print_r($users);
}else{
echo "没有数据";
}
ezSQL_mysqli()
启动新的数据库对象..
$new_db = new ezSQL_mysqli("root","root","wordpress","127.0.0.1");
$new_users = $new_db->get_results("SELECT * FROM ".$db_name->table('users'),ARRAY_A);
print_r($new_users);
debug()
显示数据表和打印执行SQL
$db->get_results("SHOW TABLES");
$db->debug();
join
连表查询使用join
$sql = "SELECT a.*,b.info FROM ".$db_name->table('users')." AS a ".
"LEFT JOIN ".$db_name->table('user_info')." AS b ".
"ON a.id = b.id ".
"WHERE a.id = 1";
$item = $db->get_row($sql,ARRAY_A);
print_r($item);
发布时间:2021/07/08
发表评论