首先數據庫基本查詢是沒有問題的
<?php
namespace app\index\controller;
use think\Db;
class Demo5
{
//1.單條查詢
public function find()
{
$res = Db::table('customers')
->field('Name,CustomerID')
->where('CustomerID', '=', 1)
->find();
dump(is_null($res) ? '沒有找到' : $res);
}
}
返回結果為:

但是當field參數為數組,需要給字段起別名時:
<?php
namespace app\index\controller;
use think\Db;
class Demo5
{
//1.單條查詢
public function find()
{
$res = Db::table('customers')
// ->field('Name,CustomerID')
->field(['CustomerID'=>'顧客編號'])
->where('CustomerID', '=', 1)
->find();
dump(is_null($res) ? '沒有找到' : $res);
}
}
卻報了以下錯誤:

(修改字段為中文時才會出現該錯誤,小白看不懂報錯...)查詢手冊發現,還有另外一種起別名的方法:
<?php namespace app\index\controller; use think\Db; class Demo5 { //1.單條查詢 public function find() { $res = Db::table('customers') // ->field('Name,CustomerID') // ->field(['CustomerID'=>'顧客編號']) ->field('CustomerID as 顧客編號') ->where('CustomerID', '=', 1) ->find(); dump(is_null($res) ? '沒有找到' : $res); } }
結果成功運行:

不禁好奇:是不支持數組參數起別名了嗎?還是說我哪里配置的不正確?
