Reputation: 165
Hi All I'm new to Codeigniter, I'm trying to get all data relating to the account_id passed from a previous page.
I'm passing the account_id but not passing the name field associated with the account_id. The name field is blank.
I'm getting an error:
Here is my code for the controller:
function input($account_id = '', $name = ''){
if((int)$account_id > 0){
$query = $this->db->select('name', $name);
$query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
$data['account'] = $query;
$data['fname']['value'] = $name;
$data['faccount_id']['value'] = $account_id;
$data['name'] = '';
$data['account_id'] = '';
}
$this->load->view('manage/input',$data);
}
Here is my input view form:
<?php
$data = array(
'name' => $fname['value'],
'account_id' => $faccount_id['value']
);
echo '<form action="/manage/edit" method="post" accept-charset="utf-8">';
echo form_hidden($data);
echo $account_id .' Account ID'.
form_input($faccount_id);
echo $name .' Name'.
form_input($fname);
$data = array('name' => 'submit', 'value' => 'Update Account', 'class' => 'submit');
echo form_submit($data);
?>
<?php echo form_close(); ?>
Upvotes: 1
Views: 12237
Reputation: 4250
Occupied from documentation:
$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.
So replace
$query = $this->db->select('name', $name);
with
$this->db->select('name', $name); // No need to assign it to a variable
Then $this->db->get_where();
executes the query and return the entire query object you need to fetch the result from it.
$query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
$result = $query->row_array(); //For single row
$result = $query->result_array(); //For more than one row
Upvotes: 2
Reputation: 165
Found the solution with the help of djjjuk.
$query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
if ($query->num_rows() > 0)
{
$row = $query->row_array();
}
$data['account'] = $row;
$data['fname']['value'] = $row['name'];
$data['faccount_id']['value'] = $account_id;
$data['name'] = '';
$data['account_id'] = '';
}
$this->load->view('manage/input',$data);
}
Upvotes: 0
Reputation: 55972
i believe get_where
just preps your query
$query->row_array()
should return your result as an array
$query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
$result = $query->row_array();
For the second part of your question it looks like there is a lot going on. What is the value of $name
in your input
function? Are you actually passing a value to input
? Make sure that name is set in your input
function or else it will just be an empty string.
Upvotes: 3