[eluser]JamesD[/eluser]
[quote author="JimmyJ" date="1197764942"]Hi, i'm trying to pass an id variable to an sql query, but gives me the error:
Error Number: 1054
Unknown column '$id' in 'where clause'
SELECT * FROM pages WHERE pageID=$id
The code is:
Code:
<?php
class Page extends Controller {
function Page()
{
parent::Controller();
}
function view()
{
/* Get Template */
$data['doctypes'] = $this->load->view('template/doctypes', '', true);
$data['head'] = $this->load->view('template/head', '', true);
$data['foot'] = $this->load->view('template/foot', '', true);
/* Get DB Content */
$id = $this->uri->segment(2);
$query = $this->db->query('SELECT * FROM pages WHERE pageID=$id');
$row = $query->row();
$data['metaTitle'] = $row->metaTitle;
$data['pageTitle'] = $row->pageTitle;
$this->load->view('page', $data);
}
}
?>
What am I doing wrong? I've looked through the database documentation but the examples are a little limited. It works fine in my head
Any ideas?[/quote]
Hello JimmyJ,
The variable is enclosed with single quotes.
Here are a couple ways to correct it, for example...
Instead of:
Code:
$query = $this->db->query('SELECT * FROM pages WHERE pageID=$id');
Try using
Code:
$query = $this->db->query('SELECT * FROM pages WHERE pageID=' . $id);
OR
Code:
$query = $this->db->query("SELECT * FROM pages WHERE pageID=$id");
-JamesD