CodeIgniter Forums

Full Version: Undefined variable (newbie)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]mlynno[/eluser]
Hi, I hope that I have picked the right forum for this. I'm new to PHP and CodeIgniter and working my way through the CodeIgniter 1.7 book trying to set up a CRUD function. I keep getting the message "Message: Undefined variable: query, Filename: models/crud_model.php,
Line Number: 56." Any advice would be greatly appreciated. BTW I am using CodeIgniter 1.7.3. Here is the code --
Model (applicable parts)
Code:
function get_record($PlantId)
    {
        if(!empty($PlantId))
        {
               $this->db->where('PlantId', $PlantId);
            $query = $this->db->get('plantdata');
                }
                if ($query->num_rows() > 0) {   // this is line 56 -- undefined $query
                        return $row = $query->row();
        }
                else
                {
                    $row = FALSE;
                }
                return $row;
    }
Here's the Controller (applicable parts)
Code:
function view_record($PlantId = '')
        {
            $this->load->model('crud_model');
            $record = $this->crud_model->get_record($PlantId);
            $data['title'] = "Record view: ";
            $data['row'] = $record;
            $this->load->view('crud/view', $data);
        }
The view never gets called, but just in case, it looks like this (abbreviated):
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;View Single Record&lt;/title&gt;
         &lt;/head&gt;
&lt;body&gt;
    <h1>&lt;?php echo $title ?&gt;</h1>
    &lt;?php
    if($row == FALSE)
    {
        echo "The record does not exist";
    }
else
    {
     ?&gt;
    <ul>
        <li>Plant ID: &lt;?php echo $row->PlantId; ?&gt;</li>
         <li>Plant Family: &lt;?php echo $row->Family; ?&gt;</li>
...much more here ... </ul>
&lt;?php
    }
    ?&gt;
&lt;/body&gt;
My thanks in advance!

El Forum

[eluser]NeoArc[/eluser]
This never runs:
$this->db->where('PlantId', $PlantId);
$query = $this->db->get('plantdata');

Because $PlantId is null or not declared.

Your action only works when editing old records.

El Forum

[eluser]mlynno[/eluser]
Thank you so much -- you pointed me in the right direction and all is well. I really appreciate your swift reply!

El Forum

[eluser]NeoArc[/eluser]
No problem