Query
This is for making a raw query against the database. The $query is the statement you need to execute. The $fetch parameter is optional only, if you want to retrieve the result of the statement just change the $fetch = "Y".
If $fetch = "Y" then add the ->get(); mehod to get the result otherwise do not put ->get();.
// DB()->query($query, $fetch = "N");
<?php
namespace App\Controllers;
class UserController
{
public function index()
{
$test_query = DB()->query('SELECT * FROM users WHERE id > 0', 'Y')->get();
return view('/index', compact('test_query'));
}
}And this is how we retrieve the result of the query:
// in your views
foreach($test_query as $test){
echo $test['fullname'];
}Another example is using the query method to update a column data in a table.
<?php
namespace App\Controllers;
class UserController
{
public function update()
{
DB()->query('UPDATE users SET fullname = 'test name only' WHERE id = 1');
}
}Last updated