Codeigniter where clause Fetch single row
![]() |
| Fetch single row from database with codeigniter where clause |
In this lesson we are going to learn how to fetch single query with Codeigniter where clause. Follow the step to learn how to fetch single query with Codeigniter where clause.
=>Database and Table:
Database Name: test
Table Name: user
SQL Codes for Create user table
CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(150) NOT NULL, `email` varchar(150) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;SQL Codes for Insert some rows in user table
INSERT INTO `user` (`id`, `username`, `email`) VALUES (NULL, 'ANJAN KUMAR', 'Anjankumardhali@gmail.com'), (NULL, 'ANJAN', 'biswasshiuli608@gmail.com'), (NULL, 'Supriya Gain', 'SupriyaStar@Yahoo.com'), (NULL, 'Priya roy', 'priya6@gmail.coma'), (NULL, 'ANJAN KUMAR', 'Anjankumardhali@gmail.com'), (NULL, 'ANJAN', 'biswasshiuli608@gmail.com'), (NULL, 'ANJANBD', 'biswasshiuli608@gmail.com');//Some Configuration:
Base Url:
go to application/config/config.php and configure look like below:
$config['base_url'] = 'http://localhost/codeigniter/';
Autoload Url:
go to application/config/autoload.php and configure look like below:
$autoload['helper'] = array('url');
Database Configuration:
application/config/database.php
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'test', //Your Database table name here
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Use Below Codes to fetch single query with Codeigniter
1. View file-- index.php
In this, we fetched all the names from data base and showed them in links.
<h2>Fetch Single Row With Codeigniter where</h2>
<table border="1" cellspacing="0">
<tr>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
<?php foreach($result as $row){?>
<tr>
<td><?php echo $row->username;?></td>
<td><?php echo $row->email;?></td>
<td><a href="<?php echo base_url();?>index.php/blog/single/<?php echo $row->id;?>">Detail</a></td>
</tr>
<?php } ?>
</table>
View File-- single.phpsingle.php file to display single row from database
<h2>Fetch Single Row With Codeigniter where</h2>
<table border="1" cellspacing="0">
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<?php foreach($single as $row){?>
<tr>
<td><?php echo $row->username;?></td>
<td><?php echo $row->email;?></td>
</tr>
<?php } ?>
</table>
<a href="http://localhost/codeigniter/index.php/Blog">Back Home</a>
2. Controller file-- Blog.php<?php
class Blog Extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('User');
}
public function index(){
$data['result']=$this->User->users();
$this->load->view('index',$data);
}
public function single(){
$id=$this->uri->segment('3');
$data['single']=$this->User->single_user($id);
$this->load->view('single',$data);
}
}
?>
3. Model File-- user.php<?php
class User extends CI_Model{
function __construct(){
parent::__construct();
$this->load->database();
}
//Fetch all Rows from user
public function users(){
$query=$this->db->get('user');
return $query->result();
}
public function single_user($id){
//Fetch Single Data from database
$this->db->select('username,email');
$this->db->from('user');
$this->db->where('id',$id);
$query=$this->db->get();
return $query->result();
}
}
?>
now run below url to watch resulthttp://localhost/codeigniter/index.php/blog
That's it how to Fetch single row from database with Codeigniter where clause .
Learn How to Remove Index.php from Codeigniter
Keep Visiting for more codes.thank you for visiting...


No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.