PHP fSQL Tutorial
PHP fSQL Tutorial This tutorial is designed to give a brief overview of the PHP fSQL API since version 1.2. The syntax and function of the SQL queries understood by fSQL will be addressed in another tutorial.
I. The Basics Every script that wishes to load fSQL needs to only include one file, the fSQL.php located in the distribution. If fSQL.php is in the same directory as the script using it, this is all you need:
require_once("fSQL.php"); Once the file is included, there is only one class you need for all of your SQL needs: fSQLEnvironment. This contains information on the different databases in the program and their location on the file system. fSQLEnvironment is a simple API that is very similar to the PHP mysql API in almost every way. For example, fSQLEnvironment's fetch_assoc() is modeled directly after mysql_fetch_assoc() so if you're having trouble understanding fSQL's documentation, the PHP mysql API could also help you understand what each function does.
To create a new fSQLEnvironment class, it has a simple constructor with no parameters:
$fsql = new fSQLEnvironment; II. Defining Databases Databases in fSQL are directories in the file system with an associated name given to them. To define one:
$fsql->define_db("mydb", "/path/to/db"); The first parameter is the database name and the second is the path to that database. This tells the environment that the database to be called "mydb" will be located in the directory /path/to/db on the file system. In other words, all table information and data for "mydb" should be loaded from and stored to the directory /path/to/db. If the supplied path does not exist, fSQL will attempt to create it and set the appropriate permissions.
As of PHP fSQL v1.2, fSQL allows you have multiple databases defined for fSQL. For example, one could define several databases like so:
$fsql->define_db("db1", "/path/to/db1"); $fsql->define_db("db2", "/path/to/db2"); $fsql->define_db("db3", "/path/to/db3"); To select which database is to be the default when using queries and other function calls, use the select_db() function:
$fsql->select_db("db2"); This function is the equivalent to the "USE `db2`" query. You should always select a default database before using any other functions in fSQLEnvironment.
III. Data Definition and Manipulation From here on out, the most important method for dealing with the databases' data is the query() method. The query method takes one parameter and that is the string fSQL query to execute. The simplest form is data definition and manipulation performs just the query and returns either a true value on sucess or a false value on failure.
$fsql->query("CREATE TABLE example( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(30), age INT, PRIMARY KEY(id) )") or die($fsql->error()); The other types of queries worth mentioning: data manipulation (like INSERT, UPDATE, etc). On these queries, the method affected_rows() returns the number of rows added or modified by the last data manipulation query. For example:
$fsql->query("DELETE FROM example WHERE id < 5") or die("DELETE failed: ".$fsql->error()); echo "Deleted Rows: ".$fsql->affected_rows(); IV. Data Selection Executing data retrieval queries like SELECT are also performed using the query() method. Except on data retrieval queries, the query method returns a handle to a result set which can be iterated through row by row using the fetch methods. Below we see an example.
$results = $fsql->query("SELECT id, name FROM example WHERE age > 30") or die("SELECT failed: ".$fsql->error()); while($row = $fsql->fetch_array($results)) { echo $row['id']." ".$row['name']."\r\n"; } $fsql->free_result($results); fetch_array($results) returns the next row in the result set until the last row has passed and then it returns NULL to stop the loop.
There are several ways to iterate through a result set:
Return the row as an associative array using the names/aliases of the columns as the row's keys: $fsql->fetch_array($results) $fsql->fetch_array($results, FSQL_ASSOC) $fsql->fetch_assoc($results) Returns the row as a normal array with integer indexes: $fsql->fetch_row($results) $fsql->fetch_array($results, FSQL_NUM) Returns the row with both column name keys and integer keys: $fsql->fetch_both($results) $fsql->fetch_array($results, FSQL_BOTH) Returns the row as "class-less object" using the names/aliases of the columns as the object's member variables: $fsql->fetch_object($results) Other result set methods of interest:
num_rows($results) - Returns the number of rows in the result set num_fields($results) - Returns the number of columns in the result set data_seek($results, $n) - Sets the internal cursor of the result set so that the next fetch method returns the nth row of the result set. free_result($results) - Frees the result set and any memory it used
PHP fSQL Tutorial的更多相关文章
- [翻译+山寨]Hangfire Highlighter Tutorial
前言 Hangfire是一个开源且商业免费使用的工具函数库.可以让你非常容易地在ASP.NET应用(也可以不在ASP.NET应用)中执行多种类型的后台任务,而无需自行定制开发和管理基于Windows ...
- Django 1.7 Tutorial 学习笔记
官方教程在这里 : Here 写在前面的废话:)) 以前学习新东西,第一想到的是找本入门教程,按照书上做一遍.现在看了各种网上的入门教程后,我觉得还是看官方Tutorial靠谱.书的弊端一说一大推 本 ...
- thrift 服务端linux C ++ 与客户端 windows python 环境配置(thrift 自带tutorial为例)
关于Thrift文档化的确是做的不好.摸索了很久才终于把跨linux与windows跨C++与python语言的配置成功完成.以下是步骤: 1) Linux下环境配置 ...
- Hive Tutorial(上)(Hive 入门指导)
用户指导 Hive 指导 Hive指导 概念 Hive是什么 Hive不是什么 获得和开始 数据单元 类型系统 内置操作符和方法 语言性能 用法和例子(在<下>里面) 概念 Hive是什么 ...
- Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python
f you are not familiar with MySQL stored procedures or want to review it as a refresher, you can fol ...
- Using FreeMarker templates (FTL)- Tutorial
Lars Vogel, (c) 2012, 2016 vogella GmbHVersion 1.4,06.10.2016 Table of Contents 1. Introduction to F ...
- Oracle Forms 10g Tutorial Ebook Download - Oracle Forms Blog
A step by step tutorial for Oracle Forms 10g development. This guide is helpful for freshers in Orac ...
- Tutorial - Deferred Rendering Shadow Mapping 转
http://www.codinglabs.net/tutorial_opengl_deferred_rendering_shadow_mapping.aspx Tutorial - Deferred ...
- anguar.js tutorial demo
http://docs.angularjs.cn/tutorial angular 入门demo : PhoneCat Tutorial App 别人的DEMO(官方版):http://angular ...
随机推荐
- 【SSH进阶之路】Hibernate搭建开发环境+简单实例(二)
Hibernate是很典型的持久层框架,持久化的思想是很值得我们学习和研究的.这篇博文,我们主要以实例的形式学习Hibernate,不深究Hibernate的思想和原理,否则,一味追求,苦学思想和原理 ...
- Swift 函数Count,Filter,Map,Reduce
原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 前言:和OC不同,Swift有非常多全局的函数,这些全局函数对简化代码来说非常实用.眼下Swift出到了2.0,只是我这篇 ...
- pageContext 获取Session 为null原因
问题:在J2EE应用中.发如今自己定义标签中取不到session: HttpSession session = pageContext.getSession(); 你会发现session的值可能是空的 ...
- UVa 1531 - Problem Bee
题目:如图所看到的的蜂巢型的图中.蜜蜂想从A点飞到B点,假设A与B不在同一个正六边形中, 则它先飞到A的中心.每次飞到相邻格子的中心,最后飞到B的中心,再飞到B点: 假设在一个格子中.直接飞过去就可以 ...
- Python循环定时服务功能(相似contrab)
Python实现的循环定时服务功能.类似于Linux下的contrab功能.主要通过定时器实现. 注:Python中的threading.timer是基于线程实现的.每次定时事件产生时.回调完响应函数 ...
- UVA 11488 Hyper Prefix Sets (Trie)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- HTTP要点概述:七,编码,压缩传输,分块传输
一,编码: HTTP 在传输数据时可以按照数据原貌直接传输,但也可以在传输过程中通过编码提升传输速率.通过在传输时编码,能有效地处理大量的访问请求.但是,编码的操作需要计算机来完成,因此会消耗更多的 ...
- ios25---图片拉伸
控制器: // // ViewController.m // 12-图片的拉伸问题 // // Created by xiaomage on 15/12/30. // Copyright © 2015 ...
- bzoj2709: [Violet 1]迷宫花园
二分答案,spfa check就行了. gb题卡精度. #include<cstdio> #include<iostream> #include<cstring> ...
- 【HDU 5698】 瞬间移动
[题目链接] 点击打开链接 [算法] 用f[i][j]表示走到(i,j)这个位置有多少种方案,因为走到(i,j)这个位置,上一步一定在它左上角的矩形中,所以, f(i,j) = sigma( f(x, ...