Example of incremental operation of database links

Source: Internet
Author: User

Initialize the database class the following code will load and initialize the database class according to your:
$this->load->database ();
Multi-result standard query (object form)
$query = $this->db->query (' SELECT name, title, email from my_table '); foreach ($query->result () as $row) {    echo $row->title;    echo $row->name;    echo $row->email;} Echo ' Total Results: '. $query->num_rows ();
the result () function above returns an array of objects. Example:   Multi-result standard query, $row (array form)
$query = $this->db->query (' SELECT name, title, email from my_table '); foreach ($query->result_array () as $row) { C0/>echo $row [' title '];    echo $row [' name '];    echo $row [' email '];}
 
the Result_array () function above returns an array with the subscript. For example: $row ['title'  test Results If your query may not return results, we recommend that you first use the Num_rows () function to test:
$query = $this->db->query ("YOUR query"), if ($query->num_rows () > 0) {   foreach ($query->result () as $ Row)   {      echo $row->title;      echo $row->name;      echo $row->body;   }}
Single-result standard query (object form)
$query = $this->db->query (' SELECT name from my_table LIMIT 1 '); $row = $query->row (); Echo $row->name;
the row () function above returns an object. Example: $row   single result standard query (array form)
$query = $this->db->query (' SELECT name from my_table LIMIT 1 '); $row = $query->row_array (); Echo $row [' name '];
the Row_array () function above returns an array. Example: $row ['name' standard insertion (insert)
$sql = "INSERT into MyTable (title, name)         VALUES (". $this->db->escape ($title). ",". $this->db->escape ($ Name). ")"; $this->db->query ($sql); Echo $this->db->affected_rows ();
 Fast Query Fast query class can provide us with a quick way to obtain data:

$query = $this->db->get (' table_name '); foreach ($query->result () as $row) {    echo $row->title;}
Shortcut Insert (INSERT)
$data = Array (               ' title ' = = $title,               ' name ' = = $name,               ' date ' = $date            ); $this->db->insert ( ' MyTable ', $data);//equivalent: INSERT into MyTable (title, name, date) VALUES (' {$title} ', ' {$name} ', ' {$date} ')

Example of incremental operation of database links

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.