PHP数据库操作使用ezSQL来实现,简单好用。

如果用的是mysql数据库,将下载的ezSQL文件中的mysql和shared连个文件夹拷贝到PHP工程目录中引用即可。

在PHP文件中

// Include ezSQL core
include_once "shared/ez_sql_core.php";
// Include ezSQL database specific component
include_once "mysql/ez_sql_mysql.php";
// Initialise database object and establish a connection
// at the same time - db_user / db_password / db_name / db_host
include_once ("Pagination.php");
$db = new ezSQL_mysql('root', 'abcdef', 'test_t', 'localhost');

使用示例:

取数值:

$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 “<b>” . ucfirst($key) . “</b>: $val<br>”;
} // Do a P between dogs..
echo “<p>”;
}
}
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)

解决ezSQL编码问题

编辑 ez_sql_mysql.php 文件,在96行添加一段代码

if(is_resource($this->dbh) && get_resource_type($this->dbh) == 'mysql link') {
@mysql_query('set names utf8',$this->dbh);
}

问题解决了!

PHP开发-最简单的数据库操作,使用ezSQL的更多相关文章

  1. Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作

    SQLite  是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...

  2. [置顶] Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作

    SQLite  是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...

  3. 在安卓开发中使用SQLite数据库操作实例

    前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...

  4. Django简单的数据库操作

    当然,本篇的前提是你已经配置好了相关的环境,这里就不详细介绍. 一. 在settings.py文件中设置数据库属性. 如下: DATABASES = { 'default': { 'ENGINE': ...

  5. Django初级手册1-项目和应用的创建与简单的数据库操作

    创建项目 django-admin.py startproject mysite 1. 目录结构 mysite/ #项目的名称 manage.py #可通过命令和项目进行交互的文件 mysite/ # ...

  6. python 简单的数据库操作之转账

    介绍:本文是关于数据库的简单操作,实现转账(只是修改数据库中用户的账户金额)的功能 模块介绍:首先是入口主函数 主函数中实现转账方法  以及异常的处理: if __name__ == "__ ...

  7. Android开发--数据存储之数据库操作

    简介: SQLite 的介绍: SQLite数据库属于文本型的数据库,它是以文本的形式来保存的.Android提供了对 SQLite 数据库的完全支持,应用程序中的任何类都可以通过名称来访问任何的数据 ...

  8. 通过一个简单的数据库操作类了解PHP链式操作的实现

    class Model{ public $table; //操作的表; private $opt; //查询的参数; private $pri; //表的主键; private $lastSql; / ...

  9. Java实例---简单的数据库操作

    源码分析 DAOFactory.java package cn.ftl.mysql ; public class DAOFactory { public static IEmpDAO getIEmpD ...

随机推荐

  1. 【转载】Java中的锁机制 synchronized & 偏向锁 & 轻量级锁 & 重量级锁 & 各自优缺点及场景 & AtomicReference

    参考文章: http://blog.csdn.net/chen77716/article/details/6618779 目前在Java中存在两种锁机制:synchronized和Lock,Lock接 ...

  2. 【明哥报错簿】之【 "javax.servlet.http.HttpServlet" was not found on the Java Build Path || HttpServletRequest/HttpServletResponse cannot be resolved to a type】

    The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path login ...

  3. 深入理解JVM一配置参数

    一.JVM配置参数分为三类参数: 1.跟踪参数 2.堆分配参数 3.栈分配参数 这三类参数分别用于跟踪监控JVM状态,分配堆内存以及分配栈内存. 二.跟踪参数 跟踪参数用于跟踪监控JVM,往往被开发人 ...

  4. Seek the Name, Seek the Fame POJ - 2752(拓展kmp || kmp)

    题意: 就是求前缀和后缀相同的那个子串的长度  然后从小到大输出 解析: emm...网上都用kmp...我..用拓展kmp做的  这就是拓展kmp板题嘛... 求出extend数组后  把exten ...

  5. UVA - 11997(巧妙的优先队列)

    题意: 有k个整数数组,各包含k个元素,在每个数组中取一个元素加起来,可以得到kk个和,求这些和中最小的k个值 解析: 从简单的情况开始分析:经典方法,对原题没有思路,那么分析问题的简化版 这是对于两 ...

  6. CF335F Buy One, Get One Free 贪心

    题意: \(n\)个物品,每个物品有一个价格,买一个高价格的物品,可以选择免费得到一个价格严格低于这个物品的物品.求得到\(n\)个物品的最小代价. 题解: 神仙贪心-- 题目要求求出最小代价,相当于 ...

  7. BZOJ3509 [CodeChef] COUNTARI 【分块 + fft】

    题目链接 BZOJ3509 题解 化一下式子,就是 \[2A[j] = A[i] + A[k]\] 所以我们对一个位置两边的数构成的生成函数相乘即可 但是由于这样做是\(O(n^2logn)\)的,我 ...

  8. 洛谷 P2498 [SDOI2012]拯救小云公主 解题报告

    P2498 [SDOI2012]拯救小云公主 题目描述 英雄又即将踏上拯救公主的道路-- 这次的拯救目标是--爱和正义的小云公主. 英雄来到\(boss\)的洞穴门口,他一下子就懵了,因为面前不只是一 ...

  9. 用centos 6.5 webservice 安装系统后的问题

    编译pcre时:缺少一下库: yum -y install gcc yum -y install gcc-c++

  10. iOS之富文本(一)

    NSAttributedString叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体.字号.字体大小等各不相同的风格,还可以对段落进行格式化. 通过以下代码即可实现上面 ...