博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
操作MySQL,使用ezSQL,简单而方便
阅读量:4466 次
发布时间:2019-06-08

本文共 3636 字,大约阅读时间需要 12 分钟。

最近使用PHP做点小东东,使用了ezSQL,真的感觉很简单很ez。

ezSQL官方下载地址:

使用示例:

取数值:

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

 

取对象:

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

 

取数组:

$users = $db->get_results("SELECT name, email FROM users");foreach ( $users as $user ){    // 使用对象语法    echo $user->name;    echo $user->email;}

可以看出,其实函数返回值为二维数组,经foreach分解后,$user为每条记录的内容,可直接用$user->字段名的方式访问。

get_results()还有另一种调用方式:

 

// Extract results into the array $dogs (and evaluate if there are any results at the same time)..if ( $dogs = $db->get_results(“SELECT breed, owner, name FROM dogs”, ARRAY_A) ){            // Loop through the resulting array on the index $dogs[n]            foreach ( $dogs as $dog_detail )            {                         // Loop through the resulting array                        foreach ( $dogs_detail as $key => $val )                        {                                    // Access and format data using $key and $val pairs..                                    echo “” . ucfirst($key) . “: $val
”; } // Do a P between dogs.. echo “

”; }}else{ // If no users were found then if evaluates to false.. echo “No dogs found.”;}

 

输出结果:

Output:

Breed: Boxer
Owner: Amy
Name: Tyson
Breed: Labrador
Owner: Lee
Name: Henry
Breed: Dachshund
Owner: Mary
Name: Jasmine

 

执行Insert操作:

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

 

调试信息

// Display last query and all associated results

$db->debug();

四种方法:

bool $db->query(query)
var $db->get_var(query)
mixed $db->get_row(query)
mixed $db->get_results(query)

ezSQL functions

$db->get_results -- get multiple row result set from the database (or previously cached results)

$db->get_row -- get one row from the database (or previously cached results)

$db->get_col -- get one column from query (or previously cached results) based on column offset

$db->get_var -- get one variable, from one row, from the database (or previously cached results)

$db->query -- send a query to the database (and if any results, cache them)

$db->debug -- print last sql query and returned results (if any)

$db->vardump -- print the contents and structure of any variable

$db->select -- select a new database to work with

$db->get_col_info -- get information about one or all columns such as column name or type

$db->hide_errors -- turn ezSQL error output to browser off

$db->show_errors -- turn ezSQL error output to browser on

$db->escape -- Format a string correctly to stop accidental mal formed queries under all PHP conditions

$db = new db -- Initiate new db object.

 

ezSQL variables

$db->num_rows – Number of rows that were returned (by the database) for the last query (if any)

$db->insert_id -- ID generated from the AUTO_INCRIMENT of the previous INSERT operation (if any)

$db->rows_affected -- Number of rows affected (in the database) by the last INSERT, UPDATE or DELETE (if any)

$db->num_queries -- Keeps track of exactly how many 'real' (not cached) queries were executed during the lifetime of the current script

$db->debug_all – If set to true (i.e. $db->debug_all = true;) Then it will print out ALL queries and ALL results of your script.

$db->cache_dir – Path to mySQL caching dir.

$db->cache_queries – Boolean flag (see mysql/disk_cache_example.php)

$db->cache_inserts – Boolean flag (see mysql/disk_cache_example.php)

$db->use_disk_cache – Boolean flag (see mysql/disk_cache_example.php)

$db->cache_timeout – Number in hours (see mysql/disk_cache_example.php)

 

转载于:https://www.cnblogs.com/richardw/archive/2012/10/06/2713027.html

你可能感兴趣的文章
提高效率必须改掉的7种习惯
查看>>
Java判断语句中判断条件的执行顺序
查看>>
Windows平台下tomcat+java的web程序持续占cpu问题调试
查看>>
OO第四次博客作业!
查看>>
HDU 吉哥系列故事——完美队形II 騰訊馬拉松初賽第二輪D題
查看>>
[转]SQL Server 性能调优(io)
查看>>
设计模式学习-每日一记(6.原型模式)
查看>>
不已0开头的数字正则
查看>>
HTML撑起浮动子元素得父元素高度
查看>>
LeetCode--018--四数之和(java)
查看>>
Redis消息队列
查看>>
电商网站架构设计
查看>>
http://jingyan.baidu.com/article/4dc40848e7b69bc8d946f127.html
查看>>
WCF netTcp配置
查看>>
数据类型转换
查看>>
Nodejs学习笔记(2) 阻塞/非阻塞实例 与 Nodejs事件
查看>>
什么是FreeMaker
查看>>
设计模式学习笔记(总结篇:模式分类)
查看>>
TCP的三次握手/建立连接
查看>>
Python 教程阅读笔记(一):使用解释器
查看>>