📕
Sprnva Docs
  • Introduction
  • What's New?
  • Installation
    • Requirements
    • Download Sprnva zip no composer required
    • Install Sprnva Using Composer
    • Setup Config
  • Deployment
  • File Structure
  • Database
    • Select
    • SelectLoop
    • Insert
    • Update
    • Delete
    • Query
    • Seeder
    • Paginate
    • With
    • andFilter
    • withCount
    • get
  • Query Builder
  • Migration
    • INSTANCES
      • NEW
      • RENAMETABLE
      • DROP
      • CHANGE
    • DUMPS
      • dump
      • dump prune
    • MAKE
    • MIGRATE
      • migrate
      • migrate fresh
    • ROLLBACK
  • Routing
    • with parameter
    • multiple parameters
    • with closure
    • group
    • controller grouping
  • Controllers
    • methods
  • Views
    • Passing Parameters
  • Validation
    • validate
      • validation type
  • Authentication
  • Alert Messages
  • Default Helpers
  • CSRF Protection
  • Email
  • Packages
    • Fortify
  • Themes and Templates
Powered by GitBook
On this page
  1. Database

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');
    }
}
PreviousDeleteNextSeeder

Last updated 2 years ago