This blog requires you to have a Laravel foundation, laravel Portal: http://blog.csdn.net/zls986992484/article/details/52755037
Laravel provides 3 Operational database modes: DB façade (original), query Builder, and eloquent ORM.
Database configuration file in the Config directory. in PHP. Open this file and find the MySQL configuration entry.
Here is a env, which actually invokes the. env file in the Laravel root directory, which stores the configuration information for the database. Open it. Modify the database information for the project.
Build your own database, where the database contains the Vipinfo table and insert some data for easy use below. The structure of the table is shown below.
As the name suggests: This table is the member table, has the member ID (primary key), the member name, the member type, the member integral and so on field.
DB façade of database operation
Create a new student controller in the App->http->controllers directory, studentcontroller.php. The studentcontroller.php code is as follows:
[PHP] view Plain copy <?php namespace app\http\controllers; Use illuminate\support\facades\db; Class Studentcontroller extends Controller {} 1. Query operations
Add a Test1 method to the student controller, which uses the static method select () of the DB class, which is a native SQL statement and returns a two-dimensional array. DD () is a method provided by Laravel to display an array in the form of a node tree. The specific code is as follows:
[PHP] view plain copy public function test1 () {$student =db::select (' select * from Vipinfo '); Returns a two-dimensional array $student var_dump ($student); Outputs the result DD ($student) in the form of a node tree; }
Routing configuration: Route::get (' test1 ', [' Uses ' => ' studentcontroller@test1 ']);
I do not know how to configure the route, you can refer to my previous blog: http://blog.csdn.net/zls986992484/article/details/52755037
URL Access: Http://localhost/laravel/public/index.php/test1, you will print out the results.
2. New Actions
New is the static method insert () of the DB class, the first parameter is an SQL statement, the second argument is an array, and the data to insert is placed in the array. Over here. is a placeholder, through the database interface layer PDO way, to prevent the purpose of SQL injection. Returns the result of execution. Returns true if the insert succeeds, or false.
[PHP] view plain copy $bool =db::insert (insert into Vipinfo (Vip_id,vip_name,vip_type,vip_fenshu) VALUES (?,?,?,?) ", [5, ' xiaoming ', ' travel ', 670]); Var_dump ($bool); Returns true if new success is added. 3. Update operation
The update uses a static method update () for the DB class, the first parameter is an SQL statement, the second argument is an array, and the elements in the array correspond to the question marks in the SQL statement. The update successfully returns TRUE.
[PHP] View plain copy