PHP using get_where clause in Codeigniter not passing data - php

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(); ?>

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.

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

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

Related

Codeigniter- Setting id of the other table be equal to id of another table

I'm stuck with setting the existing customers id be equal to the purchase id. This is model for entry.
On the else block, the error says:
"Message: Object of class CI_DB_mysqli_result could not be converted
to string"
and error on this line: "$purchase['cust_id'] = $cust;".
I'm a beginner in php and Codeigniter
Class Entry_m extends CI_Controller{
public function create_submit($customer, $purchase){
$array = array('lname' => $customer['lname'], 'fname' => $customer['fname'], 'mn' => $customer['mn']);
$this->db->like($array);
$query = $this->db->get('customer');
$count = $query->num_rows();
if($count ===0)
{
$this->db->insert('customer', $customer);
$cust_id = $this->db->insert_id();
$purchase['cust_id'] = $cust_id;
$this->db->insert('purchase', $purchase);
return $cust_id = $this->db->insert_id();
}else{
$cust = $this->db->select('cust_id')->where($array)->get('customer');
$purchase['cust_id'] = $cust;
$this->db->insert('purchase', $purchase);
}
}
}
try this
$cust = $this->db->select('cust_id')->where($array)->get('customer')->row()->cust_id;
Instead
$this->db->insert('customer', $customer);
$cust_id = $this->db->insert_id();
use
$cust_id = $this->db->insert_id('customer', $customer);

how to input the parent id into database in different table

I have table -> subkategori
and I can't insert the id_kategori attribute to table portfolio
i have a code for the model :
function get_subcategories($id_kategori){
$query = $this->db->query("SELECT id_kategori from subkategori WHERE id_subkategori=".$this->input->post('a'));
return $query->result();
}
and the controller here :
if ($this->input->post('a')!=''){
$this->load->model('Model_app');
$this->model_app->get_subcatoegories('$id_kategori');
}else{
$id_kategori = '';
}
$data = array(
'id_subkategori'=>$this->db->escape_str($this->input->post('a')),
'id_kategori'=>$id_kategori
);
$this->input->post('a') as you mentioned here I assume that you have input in the form and its name is a.
In Controller
if (!empty($this->input->post('a')))
{
$this->load->model('Model_app');
$id = $this->model_app->get_subcatoegories($this->input->post('a'));
if (empty($id)) {
$id_kategori = '';
} else {
$id_kategori = $id[0]['id_kategori'];
}
}else{
$id_kategori = '';
}
$data = array(
'id_subkategori'=>$this->db->escape_str($this->input->post('a')),
'id_kategori'=>$id_kategori
);
In Model
function get_subcategories($id_kategori)
{
$query = $this->db->query("SELECT id_kategori FROM subkategori WHERE id_subkategori = $id_kategori");
return $query->result_array();
}
You can get last insterted if with $this->db->insert_id();
In your, if statement you didn't assign the database value to the $id_kategori variable. Hence while inserting the empty value into the table it inserts as 0(int field).
And also when you pass a variable to a function it should not be single quoted. You have to pass the post variable but you are passing a variable which is undefined.
if ($this->input->post('a')!=''){
$this->load->model('Model_app');
$category = $this->model_app->get_subcatoegories($this->input->post('a'));
$id_kategori = $category['id_kategori];
}else{
$id_kategori = '';
}
$data = array(
'id_subkategori'=>$this->db->escape_str($this->input->post('a')),
'id_kategori'=>$id_kategori
);

How do i save a "name" from a table into a variable in PDO [duplicate]

I am trying to build a web application using PHP and I am using Memcached for storing user data from the database.
For example, let’s say that I have this code:
$sql = "SELECT * FROM users WHERE user_id = :user_id";
$stmt = $this->_db->prepare($sql);
$result = $stmt->execute(array(":user_id" => $user_id));
$user = $stmt->fetch(PDO::FETCH_ASSOC);
I am not really sure how to read the $user variable and get the data out of it. I will need to be able to read the email and password column.
How does this work?
PDOStatement::fetch returns a row from the result set. The parameter PDO::FETCH_ASSOC tells PDO to return the result as an associative array.
The array keys will match your column names. If your table contains columns 'email' and 'password', the array will be structured like:
Array
(
[email] => 'youremail#yourhost.com'
[password] => 'yourpassword'
)
To read data from the 'email' column, do:
$user['email'];
and for 'password':
$user['password'];
Loop through the array like any other associative array:
while($data = $datas->fetch(PDO::FETCH_ASSOC)){
print $data['title'] . '<br>';
}
or
$resultset = $datas->fetchALL(PDO::FETCH_ASSOC);
echo '<pre>' . $resultset . '</pre>';
Method
$user = $stmt->fetch(PDO::FETCH_ASSOC);
returns a dictionary. You can simply get email and password:
$email = $user['email'];
$password = $user['password'];
Other method
$users = $stmt->fetchall(PDO::FETCH_ASSOC);
returns a list of a dictionary
PDO:FETCH_ASSOC puts the results in an array where values are mapped to their field names.
You can access the name field like this: $user['name'].
I recommend using PDO::FETCH_OBJ. It fetches fields in an object and you can access like this: $user->name
To read the result you can read it like a simple PHP array.
For example, getting the name can be done like $user['name'], and so on. The method fetch(PDO::FETCH_ASSOC) will only return one tuple though. If you want to get all tuples, you can use fetchall(PDO::FETCH_ASSOC). You can go through the multidimensional array and get the values just the same.
Design Pattern "table-data gateway"
class Gateway
{
protected $connection = null;
public function __construct()
{
$this->connection = new PDO("mysql:host=localhost; dbname=db_users", 'root', '');
}
public function loadAll()
{
$sql = 'SELECT * FROM users';
$rows = $this->connection->query($sql);
return $rows;
}
public function loadById($id)
{
$sql = 'SELECT * FROM users WHERE user_id = ' . (int) $id;
$result = $this->connection->query($sql);
return $result->fetch(PDO::FETCH_ASSOC);
// http://php.net/manual/en/pdostatement.fetch.php //
}
}
Print all row with column 'user_id' only
$gateway = new Gateway();
$users = $gateway->loadAll();
$no = 1;
foreach ($users as $key => $value) {
echo $no . '. ' . $key . ' => ' . $value['user_id'] . '<br />';
$no++;
}
Print user_id = 1 with all column
$user = $gateway->loadById(1);
$no = 1;
foreach ($user as $key => $value) {
echo $no . '. ' . $key . ' => ' . $value . '<br />';
$no++;
}
Print user_id = 1 with column 'email and password'
$user = $gateway->loadById(1);
echo $user['email'];
echo $user['password'];
consider the following code script, will help.
$stm = $accountdb->query($sql);
$result = $stm->fetchAll(PDO::FETCH_ASSOC);
$number = $stm->rowCount();
$json = json_encode($result, JSON_UNESCAPED_UNICODE);
header("Content-type: application/json");
echo '{"total" : "' . $number . '","records" : ' . $json . '}';

select Name from database where emali = $emali?

I need select Name from databese where Email = $email;
if ($result) {
$Name = $this->db->select('Name');
$this->db->from('users');
$this->db->where('Email',$Email);
$sess_array = array(
'Name' => $Name
);
$this->session->set_userdata('logged_in',$sess_array);
print_r($sess_array);
}
You need to fetch Name from database then assign to your session
if ($result) {
$this->db->select('Name');
$this->db->from('users');
$this->db->where('Email',$Email);
$query=$this->db->get();
$result=$query->row(); // fetch single data Name
$sess_array = array(
'Name' => $result->Name // set Name to array
);
$this->session->set_userdata('logged_in',$sess_array);
}
Try this,
if ($result) {
$this->db->select('Name');
$query = $this->db->get_where('users',array('Email' => $Email));
$result = $query->row();
$sess_array = array(
'Name' => $result->Name
);
$this->session->set_userdata('logged_in',$sess_array);
print_r($sess_array);
}
also refer CodeIgniter Select Query

How to read "fetch(PDO::FETCH_ASSOC);"

I am trying to build a web application using PHP and I am using Memcached for storing user data from the database.
For example, let’s say that I have this code:
$sql = "SELECT * FROM users WHERE user_id = :user_id";
$stmt = $this->_db->prepare($sql);
$result = $stmt->execute(array(":user_id" => $user_id));
$user = $stmt->fetch(PDO::FETCH_ASSOC);
I am not really sure how to read the $user variable and get the data out of it. I will need to be able to read the email and password column.
How does this work?
PDOStatement::fetch returns a row from the result set. The parameter PDO::FETCH_ASSOC tells PDO to return the result as an associative array.
The array keys will match your column names. If your table contains columns 'email' and 'password', the array will be structured like:
Array
(
[email] => 'youremail#yourhost.com'
[password] => 'yourpassword'
)
To read data from the 'email' column, do:
$user['email'];
and for 'password':
$user['password'];
Loop through the array like any other associative array:
while($data = $datas->fetch(PDO::FETCH_ASSOC)){
print $data['title'] . '<br>';
}
or
$resultset = $datas->fetchALL(PDO::FETCH_ASSOC);
echo '<pre>' . $resultset . '</pre>';
Method
$user = $stmt->fetch(PDO::FETCH_ASSOC);
returns a dictionary. You can simply get email and password:
$email = $user['email'];
$password = $user['password'];
Other method
$users = $stmt->fetchall(PDO::FETCH_ASSOC);
returns a list of a dictionary
PDO:FETCH_ASSOC puts the results in an array where values are mapped to their field names.
You can access the name field like this: $user['name'].
I recommend using PDO::FETCH_OBJ. It fetches fields in an object and you can access like this: $user->name
To read the result you can read it like a simple PHP array.
For example, getting the name can be done like $user['name'], and so on. The method fetch(PDO::FETCH_ASSOC) will only return one tuple though. If you want to get all tuples, you can use fetchall(PDO::FETCH_ASSOC). You can go through the multidimensional array and get the values just the same.
Design Pattern "table-data gateway"
class Gateway
{
protected $connection = null;
public function __construct()
{
$this->connection = new PDO("mysql:host=localhost; dbname=db_users", 'root', '');
}
public function loadAll()
{
$sql = 'SELECT * FROM users';
$rows = $this->connection->query($sql);
return $rows;
}
public function loadById($id)
{
$sql = 'SELECT * FROM users WHERE user_id = ' . (int) $id;
$result = $this->connection->query($sql);
return $result->fetch(PDO::FETCH_ASSOC);
// http://php.net/manual/en/pdostatement.fetch.php //
}
}
Print all row with column 'user_id' only
$gateway = new Gateway();
$users = $gateway->loadAll();
$no = 1;
foreach ($users as $key => $value) {
echo $no . '. ' . $key . ' => ' . $value['user_id'] . '<br />';
$no++;
}
Print user_id = 1 with all column
$user = $gateway->loadById(1);
$no = 1;
foreach ($user as $key => $value) {
echo $no . '. ' . $key . ' => ' . $value . '<br />';
$no++;
}
Print user_id = 1 with column 'email and password'
$user = $gateway->loadById(1);
echo $user['email'];
echo $user['password'];
consider the following code script, will help.
$stm = $accountdb->query($sql);
$result = $stm->fetchAll(PDO::FETCH_ASSOC);
$number = $stm->rowCount();
$json = json_encode($result, JSON_UNESCAPED_UNICODE);
header("Content-type: application/json");
echo '{"total" : "' . $number . '","records" : ' . $json . '}';

Categories