-4
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: id

Filename: models/singlequery_model.php

Line Number: 4

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

SELECT * FROM patient WHERE id =

Filename: C:\xampp\htdocs\login\system\database\DB_driver.php

Line Number: 330

Is this the right way to query a single result?

    <?php
class Singlequery_model extends CI_Model{
    function get_query(){
        $query = $this->db->query("SELECT * FROM patient WHERE id  = $id");

        if($query->num_rows() > 0 ){
            foreach ($query->result() as $row){
                $data[] = $row;
            }
            return $data;
        }
    }
}

Below is my controller

    <?php 
class Site extends CI_Controller{
    function __construct(){
      parent::__construct();
      $this->is_logged_in();
      $this->load->model('site_model');

    }
    function delete(){
      $this->site_model->delete_row();
      redirect('site/members_area');
    }

    function view(){

        $this->load->model('singlequery_model');

        $data['records'] = $this->singlequery_model->get_query();
        $this->load->view('patient_update', $data);

    }

}

Dan
  • 9,391
  • 5
  • 41
  • 73
  • 2
    And what problem do you have? You have line `$query = $this->db->query("SELECT * FROM patient WHERE id = $id");` and Singlequery_model you don't have defined `$id` – Marcin Nabiałek Jul 21 '14 at 18:45

1 Answers1

2
$query = $this->db->query("SELECT * FROM patient WHERE id  = $id");

$id is not defined in your class

for this, make sure you call the function with $id:

$data['records'] = $this->singlequery_model->get_query($id);

I have no way of knowing where your $id comes from, so you're on your own to manage that.

  • I was asking what's the correct way to query because I got that error even I put function get_query($id){ $query = $this->db->query("SELECT * FROM patient WHERE id = $id"); if($query->num_rows() > 0 ){ foreach ($query->result() as $row){ $data[] = $row; } return $data; } } } – John Daniel Vogue Jul 21 '14 at 19:34
  • @JohnDanielVogue please update your question with your new not-working code, I'll update my answer accordingly. But it is not enough to only put `function get_query($id)` you must add it to the function *call* as well – Félix Adriyel Gagnon-Grenier Jul 21 '14 at 19:35