PHP Fsql Tutorial

Source: Internet
Author: User

PHP fsql Tutorial This Tutorial are 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 would be addressed in another tutorial.

I. The Basics every script, wishes to load Fsql needs to only include one file, the fsql.php located in the Distributi On. If fsql.php is in the same directory as the script using it, this is all of you need:

Require_once ("fsql.php"); Once the file is included, there are only one class need for all of your SQL needs:fsqlenvironment. This contains information the "different databases in the" and "their location" on the file system. Fsqlenvironment is a simple API which is very similar to the PHP MySQL API in almost every. For example, Fsqlenvironment's Fetch_assoc () is modeled directly after MYSQL_FETCH_ASSOC () so if you're having trouble und Erstanding fsql ' s documentation, the PHP MySQL API could also help you understand what each function does.

To create a new Fsqlenvironment class, it had a simple constructor with no parameters:

$fsql = new Fsqlenvironment; II. Defining Databases Databases in Fsql is directories in the file system with a 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 are the path to that database. This tells the environment, the database to be called "MyDB" would be located in the directory/path/to/db on the file System. In other words, all table information and data for "MyDB" should is loaded from and stored to the directory/path/to/db. If the supplied path does not exist, Fsql would attempt to create it and set the appropriate permissions.

As of PHP Fsql v1.2, Fsql allows you has 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 was to being the default when using queries and other function calls, use the select_db () function:

$fsql->select_db ("DB2"); This function was the equivalent to the "Use ' DB2 '" query. You should always select a default database before using a and 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's 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 (+), 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 qu ery. 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 is also performed using the query () method. Except on data retrieval queries, the query method returns a handle to a result set which can is iterated through row by R ow using the fetch methods. Below we see an example.

$results = $fsql->query ("Select ID, name from example WHERE Age >") 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 have passed and then it returns NULL to Sto P The loop.

There is 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 w ith integer indexes: $fsql->fetch_row ($results) $fsql->fetch_array ($results, Fsql_num) Returns the row with both Co Lumn name keys and integer keys: $fsql->fetch_both ($results) $fsql->fetch_array ($results, Fsql_both) Returns the RO W 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, the next fetch method returns The nth row of the result set. Free_result ($results)-frees the result set and any memory it used

Advertising trading Platform

PHP Fsql Tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.