Zend Framework Database Summary [Original]_php tutorial

Source: Internet
Author: User
Tags php foreach zend framework
zend_db Database Knowledge
Example:
Model file:
$this->fetchall ("Is_jian=1", "id DESC", 0,2)->toarray ();//According to Is_jian=1, the first 2 records are sorted in reverse order The ASC is ordered directly by the reverse ID.
Routing files:
$video =new Video ();//Instantiate the database class
$this->view->get2video = $video->get2video ();//Fetch to 2 home recommended data
index.phtml file:
Get2video as $video):?>



Add quotation marks to prevent database attacks
Quote Usage
$value = $db->quote (St John "s wort);
$value now becomes "St John" s Wort (note the quotes on both sides)
Array quotation marks
$value = $db->quote (Array (A, B, c));
$value now becomes "a", "B", "C" ("," delimited string)
Quoteinto usage
echo $where = $db->quoteinto (id =?, 1);
$where now for id = "1" (note the quotation marks on both sides)
To enclose an array in a where statement
$where = $db->quoteinto (id in (?), Array (1, 2, 3));
$where now ID in ("1", "2", "3") (a comma-delimited string)
(1) Data Query Summary
directly to the query. (using the full SQL statement)
function Quoteinto ($text, $value, $type = null, $count = NULL)
$db = $this->getadapter ();
$sql = $db->quoteinto (SELECT * from ' m_video ' WHERE ' is_guo ' =?, 1);
$result = $db->query ($sql);
Use the Pdostatement object $result to put all the result data into an array
$videoArray = $result->fetchall ();
Fetchall usage
Fetchall ($where = null, $order = NULL, $count = NULL, $offset = NULL)
Retrieves the values of all fields in the result set, returns as a continuous array, and writes NULL if the parameter is not set
You can retrieve the specified number of bars for the result set
$videoArray = $this->fetchall ("Is_jian=1 and Is_guo=1", "id DESC", 0,2)->toarray ();

FETCHASSOC usage
Fetchassoc ($sql, $bind = Array ())
Retrieves the values of all fields in the result set, returns as an associative array, and the first field as a code
$db = $this->getadapter ();
$videoArray = $db->FETCHASSOC ("select * from M_video WHERE ' Is_jian ' =: title", Array (title = 1));

Fetchcol usage
Fetchcol ($sql, $bind = Array ())
Retrieve the first field name of all result rows
$db = $this->getadapter ();
$videoArray = $db->fetchcol ("Select name from M_video WHERE ' Is_jian ' =: title", Array (title = 1));

Fetchone usage
Fetchone ($sql, $bind = Array ())
Retrieve only the first field value
$db = $this->getadapter ();
echo $videoArray = $db->fetchone ("SELECT count (*) from M_video WHERE ' Is_jian ' =: title", Array (title = 1));

Fetchpairs usage
Fetchpairs ($sql, $bind = Array ())
Retrieve a related array, the first field value is a code (ID), the second field is a value (name)
Returns: Array ([1] = 12 Zodiac [2] = Peach blossom), 1, 2: ID field.
$db = $this->getadapter ();
$videoArray = $db->fetchpairs ("Select ID, name from m_video WHERE Is_jian =: title", Array (title = 1));

Fetchrow Usage
Fetchrow ($where = null, $order = NULL)
Retrieve only the first row of the result set
$videoArray = $this->fetchrow ("Is_jian=1 and is_guo=1", id DESC)->toarray ();
Query usage
function query ($sql, $bind = Array ())
$db = $this->getadapter ();
$result = $db->query (SELECT * from ' m_video ');
$result = $db->query (SELECT * from ' m_video ' WHERE ' name ' =? and id =?, Array (12 Zodiac, 1));
$result->setfetchmode (zend_db::fetch_obj);//fetch_obj is the default value, Fetch_num,fetch_both
while ($row = $result->fetch ()) {
echo $row [name];
//}
$rows = $result->fetch ();
$rows = $result->fetchall ();
$obj = $result->fetchobject ();//echo $obj->name;
echo $Column = $result->fetchcolumn (0);//Get the first field of the result set, such as 0 for the ID number, for the case of a single field
Print_r ($rows);
Select usage
$db = $this->getadapter ();
$select = $db->select ();
$select->from (M_video, Array (id,name,clicks))
->where (Is_guo =: Is_guo and name =: name)
->order (name)//What sort sequence to participate in an array (multiple fields) or a string (a field)
->group ()//Grouping
->having ()//conditions for packet query data
->DISTINCT ()//no parameter, remove the duplicate value. Sometimes the same result as GroupBy returns
->limit (10);
//Read results using binding parameters
$params = Array (Is_guo = 1,name=> 12 zodiac);
$sql = $select->__tostring ();//Get query statement, available for debugging
$result = $db->fetchall ($select, $params);
Query to execute SELECT
$stmt = $db->query ($select);

$result = $stmt->fetchall ();
or with
$stmt = $select->query ();
$result = $stmt->fetchall ();
If you directly use
$db->fetchall ($select) as a result
Multi-table Federated query usage
$db = $this->getadapter ();
$select = $db->select ();
$select->from (M_video, Array (id,name,pic,actor,type_id,up_time))
->where (Is_guo =: Is_guo and Is_jian =: Is_jian)
->order (Up_time)
->limit (2);
$params = Array (Is_guo = 1,is_jian=>1);
$select->join (m_type, m_video.type_id = m_type.t_id, type_name);//multi-table union query
$videoArray = $db->fetchall ($select, $params);
Find () method, you can use the primary key value to retrieve data in the table.
SELECT * FROM round_table WHERE id = "1"
$row = $table->find (1);
SELECT * FROM round_table WHERE ID in ("1", "2", 3 ")
$rowset = $table->find (Array (1, 2, 3));

(2) Data deletion summary
The first method: You can delete any table
//quoteinto ($text, $value, $type = null, $count = null)
$table = m_ video;//set the table that needs to delete data
$db = $this->getadapter ();
$where = $db->quoteinto (name =?, CCC);//The Where condition statement that deletes the data
echo $rows _affected = $db->delete ($table, $where);//delete The number of rows that are affected by the data
The second method: You can only delete
//delete usage in this table
//delete ($where)
$where = "name = BBB";
echo $this->delete ($where)///delete data and get affected by the number of rows
(3) Data Update summary
The first method: You can update any table
//with "column name" and "Data" Format to update the array, update the data row
$table = m_video;//Updated data table
$db = $this->getadapter ();
$set = Array (
name = Shadow Heavy,
clicks = 888,
);
$where = $db->quoteinto (id =?, ten);//WHERE statement
//Update table data, returns the number of updated rows
echo $rows _affected = $db->update ($table, $se T, $where);

The second method: You can only update
$set = array in this table (
Name + butterfly shadow,
clicks = 8880,
);
$db = $this->getadapter ();
$where = $db->quoteinto (id =?, ten);//Where statement
$rows _affected = $this->update ($set, $where);//Update table data, Returns the number of rows updated
(4) Data Insertion summary
The first way: You can insert data into any table
$table = m_gao;//data table for inserting data
$db = $this->getadapter ();
Construct the insert array in the format format "column name" and "data", insert the data row
$row = Array (
title = = Everyone. 111,
CONTENT = TV network to be changed to use Zend Framework development Ah,
time = 2009-05-04 17:23:36,
);
Inserts a data row and returns the number of rows inserted
$rows _affected = $db->insert ($table, $

http://www.bkjia.com/PHPjc/486321.html www.bkjia.com true http://www.bkjia.com/PHPjc/486321.html techarticle zend_db Database Knowledge Example: Model file: $this-fetchall (Is_jian=1,id desc,0,2)-toarray ();//According to is_jian= 1, the first 2 records in reverse order by ID when the first argument is null, the straight ...

  • Related Article

    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.