PHP開發-最簡單的資料庫操作,使用ezSQL

來源:互聯網
上載者:User

標籤:generated   cci   term   current   val   最簡   cal   and   between   

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‘,‘[email protected]‘)");

 

調試資訊

// 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)

PHP開發-最簡單的資料庫操作,使用ezSQL

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.