The ThinkPHP 3.2. 3 framework implements a sample method for executing native SQL statements

  • 2021-12-05 05:42:40
  • OfStack

This article illustrates how the ThinkPHP 3.2. 3 framework implements the execution of native SQL statements. Share it for your reference, as follows:

"Query statement" query method

Example: Query the article title title field in the blog_article table


// Structure sql Statement 
$sql = "select `title` from blog_article";
// Or two of the following, the currently set table prefix will be automatically read 
//$sql = "select `title` from __PREFIX__article";
//$sql = "select `title` from __ARTICLE__";
// Instantiation model Object that executes the query Method to get the query data result set 
$res = M()->query($sql);

"Add, modify, delete statements" execute method

Example: Modify the title field of the article title in the blog_article table where id is 1 to "PHP is the best language in the world"


// Structure sql Statement 
$sql = "update blog_article set title='PHP Is the best language in the world ' where id=1";
// Or two of the following, the currently set table prefix will be automatically read 
//$sql = "update __PREFIX__article set title='PHP Is the best language in the world ' where id=1";
//$sql = "update __ARTICLE__ set title='PHP Is the best language in the world ' where id=1";
// Instantiation model Object that executes the execute Method to return the number of rows affected 
$res = M()->execute($sql);

Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: