namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class Student extends Controller
{
public function mql()
{
return 'my first mysql';
}
}
然后测试一下是否能输出
结果如下:
数据库的查找
代码如下:
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class Student extends Controller
{
public function mql()
{
$Students = DB::select('select * from student');//查找一个名为student的数据表,因为在env里我们已经定义了数据库的名字是laravel,而student存放在laravel里,所以我们给个student即可
echo "<pre>";
print_r($Students);
echo "</pre>";
}
}
然后将它输出,效果如下:
因为他并没有值 数据库的新增
代码如下:
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class Student extends Controller
{
public function mql()
{
$Nadded = DB::insert('insert into student(name,age) values(?,?)',['Rarin',16]);//这里我们定义在student里的name,age新增值,values两个问号将name和Rarin,age和16连接起来
echo "<pre>";
print_r($Nadded);
echo "</pre>";
}
}
效果如下图:
因为我是点击了两次刷新,所以新增了两个数据 数据库的修改
代码如下:
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class Student extends Controller
{
public function mql()
{
$update= DB::update('update student set id = ? where age = ?',[1002,16]);//这里我设置的是,如果age的值是16的时候,那么我就把id的值改成1002
echo "<pre>";
print_r($update);
echo "</pre>";
}
}
效果自行演示 数据库的删除
代码如下:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function test1()
{
$delete = DB::delete('delete from student where id > ?',[1003]);//这里指的是,删除id大于1003的数据
echo "<pre>";
print_r($delete);
echo "</pre>";
}
}
?>