- 此问题在此处已有答案**:
Direct SQL query from Model in Codeigniter 4(1个答案)
3天前关闭。
我想将表中的所有供应商记录显示到视图中,但当我使用print_r
显示数据时,它总是返回此错误,尽管表名和数据库正确,但它不会显示表内容。
它总是输出此错误:
object(CodeIgniter\Database\MySQLi\Result)#81 (8) { ["connID"]=> object(mysqli)#78 (18) { ["affected_rows"]=> int(6) ["client_info"]=> string(13) "mysqlnd 8.2.0" ["client_version"]=> int(80200) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(3) ["host_info"]=> string(20) "localhost via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(15) "10.4.27-MariaDB" ["server_version"]=> int(100427) ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(115) ["warning_count"]=> int(0) } ["resultID"]=> object(mysqli_result)#80 (5) { ["current_field"]=> int(0) ["field_count"]=> int(3) ["lengths"]=> NULL ["num_rows"]=> int(6) ["type"]=> int(0) } ["resultArray"]=> array(0) { } ["resultObject"]=> array(0) { } ["customResultObject"]=> array(0) { } ["currentRow"]=> int(0) ["numRows":protected]=> NULL ["rowData"]=> NULL }
我做错了什么?
供应商控制员:
<?php
namespace App\Controllers;
use App\Models\SupplierModel;
use CodeIgniter\API\ResponseTrait;
class SupplierController extends BaseController
{
use ResponseTrait;
function __construct()
{
$this->supplierModel = new SupplierModel();
}
public function index()
{
$model = new SupplierModel();
$dataSupplier = $model->get();
var_dump($dataSupplier);
// return view('pages/Supplier');
}
}
供应商型号:
<?php
namespace APP\Models;
use CodeIgniter\Model;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Exceptions;
use CodeIgniter\Exceptions\PageNotFoundException;
use Exception;
use CodeIgniter\Config\Database;
class SupplierModel extends Model
{
use ResponseTrait;
protected $table = 'supplier';
protected $primaryKey = 'id';
protected $allowedFields = ['name', 'vendor'];
public function get()
{
$db = \Config\Database::connect();
$builder = $db->table('supplier');
$data = $builder->get();
return $data;
}
}
数据库和表:
. env内容:
1条答案
按热度按时间q7solyqu1#
你必须生成查询结果。更新你的模型get()函数。也检查这个https://codeigniter.com/user_guide/database/results.html