Query Builder to get single value row, example Budi

#1

Morning, I want to extract and get name in Users Table, example Budi.
below my Code

<?php
//UsersController.php

namespace App\Controller;

use App\Controller\AppController;
use App\Model\Table\UsersTable;
use Cake\Event\event;
use Cake\I18n\Time;
use Cake\ORM\TableRegistry;

class UsersController extends AppController
{
  public function index()
  {
        $query = $users
	   ->find()
	   ->select(['ID', 'Nama'])
	   ->where(['Nama' => 'Budi']);

	foreach($query as $users) {
		$this->set(compact('users'));
	}
   }

  //code frome cake bake all
}
?>

the message error shown here

Notice (8): Undefined variable: users [APP/Controller\UsersController.php, line 21]

Notice (1024): Undefined property: ErrorController::$Auth in C:\xampp\htdocs\klinikucing\src\Controller\AppController.php on line 57 [CORE\src\Controller\Controller.php, line 388]

Warning (512): Unable to emit headers. Headers sent in file=C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php line=853 [CORE\src\Http\ResponseEmitter.php, line 48]

Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 148]

Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 181]

Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 181]

An Internal Server Error Occurred

I hope someone can help me, thanx

0 Likes

#2

You haven’t defined a variable called $users (It’s right there in your error messages). You should be using $this->Users instead.

0 Likes