PHP Development-The simplest database operation, using Ezsql

Source: Internet
Author: User
Tags php database

PHP database operation using Ezsql to achieve, simple and easy to use.

If you are using a MySQL database, copy the downloaded MySQL and shared folders from the Ezsql file to the PHP project directory for reference.

In the PHP file

//Include Ezsql CoreInclude_once"shared/ez_sql_core.php"; //Include ezsql Database Specific componentInclude_once"mysql/ez_sql_mysql.php"; //initialise Database object and establish a connection//At the same time-db_user/db_password/db_name/db_hostInclude_once ("pagination.php"); $db=NewezSQL_mysql ('Root','abcdef','test_t','localhost');

Examples of Use:

To take a value:

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

Fetch object:

$user = $db->get_row ("Select Name,email from users WHERE id = 2");

To take an array:

$users = $db->get_results ("Select name, email from users"), foreach ($users as $user) {    //Use object Syntax    echo $user-&G T;name;    echo $user->email;}

As can be seen, in fact, the function return value is a two-dimensional array, after the decomposition of foreach, $user the contents of each record, can be directly accessed by the $user-> field name.

There is another way to call Get_results ():

Extract results into the array $dogs (and evaluate if there is any results at the same time): if ($dogs = $db->get_results ("Select Breed, owner, name from Dogs", array_a) {            //Loop through the resulting ARRA Y 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 Result:

Output:
Breed:boxer
Owner:amy
Name:tyson

Breed:labrador
Owner:lee
Name:henry

Breed:dachshund
Owner:mary
Name:jasmine

To perform an insert operation:

$db->query ("INSERT into users (ID, name, email) VALUES (NULL, ' Justin ', ' [email protected] ') ');

Debugging information

Display last query and all associated results

$db->debug ();

Four methods:

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, were returned (by the database) in 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 what many ' real ' (not cached) queries were executed during the LIFET IME of the current script

$db->debug_all –if set to True (i.e. $db->debug_all = true;) Then it'll 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)

PHP Development-The simplest database operation, using Ezsql

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.