Select query in codeigniter not working with mysqli - codeigniter

Mysql query executed in mysql but not in mysqli and mysql is deprecated so what syntax I have to use for mysqli in codeigniter:
$sql = "SELECT admin_email
FROM `tbl_admin`
WHERE `admin_email` = '" . $username . "' and `admin_password` = '" . $password . "'";
$query = $this->db->query($sql);

You can use Codeigniter Query Bulider
$this->db->select('admin_email');
$this->db->from('tbl_admin');
$this->db->where('admin_email', $username);
$this->db->where('admin_password', $password);
$query = $this->db->get();

I hope it works...
$this->db->select('*');
$this->db->from('tbl_admin');
$this->db->where('admin_email', $username);
$this->db->where('admin_password', $password);
$query = $this->db->get();
$result = $query->row();
return $result;

Try ...
$this->db->select('admin_email');
$this->db->where(
array(
'admin_email' => $username,
'admin_password' => $password
));
$query = $this->db->get('tbl_admin');
$result = $query->row();
Hopefully it works in your end...

$this->db->where('admin_email',$email);
$this->db->where('admin_password',$password);
$qry = $this->db->get('tbl_admin');
if($qry->num_rows() > 0){
return true;
}else{
return false;
}

It will work fine only if you set your Query in the Model then load that model in your controller. Directly setting the query from Controller seems to have this issue.
Example:
From the Model: Example 'Users.php'
class Users extends CI_Model {
public function __construct() {
parent::__construct();
}
function check_username($username) {
$query = $this->db->query("select * from `user_login` where `username`='$username'")
$query_result = $query->result_array;
if (!empty($query_result)) {
return $query_result;
}
return[];
}
}
From the Controller: Example 'Userauth.php'
class Userauth extends CI_Controller {
public function process_forgot(){
$CI =& get_instance();
$CI->load->model('Users'); //Model loaded here
$username = $this->input->post('username');
$check = $CI->Users->check_username($username);
echo '<pre>';
print_r($check);
echo '</pre>';
}
}
Try it.

Related

Undefined index: email in codeigniter

im trying to log in but i keep encountering the error below. attached is my controller and model
controller
public function login(){
//load session library
$this->load->library('session');
$this->load->model('Model_students');
$email = $_POST['email'];
$password = $_POST['password'];
$data = $this->Model_students->login($email, $password);
if($data)
{
$id=$data[0]->id;
$first_name=$data[0]->firstname;
$last_name=$data[0]->lastname;
$this->session->set_userdata('user_id',$id);
$this->session->set_userdata('lname',$last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname',$first_name);
$this->getImg();
redirect('Students/homepage');
}
else{
$this->session->set_flashdata('error','Invalid login. User not found');
}
}
model
public function login($email,$password)
{
// $query = $this->db->get_where('users', array('email'=>$email, 'password'=>$password));
$query = $this->db->get_where('users', array('email'=>$email));
if($query->num_rows() == 1 )
{
return $query->result();
}
wheni i run this i get an error:
Message: Undefined index: email
Filename: controllers/Students.php
Line Number: 145
please don't use $_POST parameter without validation. use input library
public function login(){
//load session library
$this->load->library('session');
$this->load->model('Model_students');
$post = $this->input->post(NULL, TRUE);
$email = isset($post['email']) ? $post['email'] : '';
$password = isset($post['password']) ? $post['password'] : '';
$data = $this->Model_students->login($email, $password);
if($data && !empty($data)){
$id = $data[0]->id;
$first_name = $data[0]->firstname;
$last_name = $data[0]->lastname;
$this->session->set_userdata('user_id', $id);
$this->session->set_userdata('lname', $last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname', $first_name);
$this->getImg();
redirect('Students/homepage');
}else{
$this->session->set_flashdata('error', 'Invalid login. User not found');
}
}

Cannot Update On Laravel 5.5

I get problem.
This is my controller
public function finish(Request $request)
{
$result = $request->input('data');
//$data = json_decode($result, true);
return $this->InvoiceBayar($result);
}
public function InvoiceBayar($result)
{
$data = json_decode($result, true);
$transaction = $data['transaction_status'];
$type = $data['payment_type'];
$order_id = $data['order_id'];
$fraud = $data['fraud_status'];
Fee::where('invoice',$order_id)
->update([
'status' => 'Paid',
]);
echo "Transaction order_id: " . $order_id ." successfully transfered using " . $type;
}
This is my Route
Route::POST('/notification', 'SnapController#finish');
When Payment gateway, send a parameter to me, I cannot update DB.
But when I use POSTMAN. I success update DB
You need to use $request->all() as it will contain all payment gateway data.
public function finish(Request $request)
{
$result = $request->all();
return $this->InvoiceBayar($result);
}
Alternately you can do this
$update = Fee::where('invoice',$order_id)->first();
$update->status = 'Paid';
$update->save();
You should try this:
public function InvoiceBayar($result)
{
$data = json_decode($result, true);
$transaction = $data->transaction_status;
$type = $data->payment_type;
$order_id = $data->order_id;
$fraud = $data->fraud_status;
Fee::where('invoice',$order_id)
->update([
'status' => 'Paid',
]);
echo "Transaction order_id: " . $order_id ." successfully transfered using " . $type;
}

CodeIgniter pass variables form controller to model

Ok I want to pass two variables from a controller to a model but I get some kind of error. Am I passing variables on right way? My syntax is:
Controller:
public function add_tag(){
if(isset($_POST['id_slike']) && isset($_POST['id_taga'])){
$slika = $_POST['id_slike'];
$tag = $_POST['id_taga'];
$this->load->model("Member_model");
$res = $this->Member_model->add_tags($slike, $tag);
foreach ($res->result() as $r){
echo $r->name;
}
}
else{
echo "";
}
}
Model:
public function add_tags(){
$data = array(
'tags_id' => $tag ,
'photos_id' => $slika
);
$check = $this->db->query("SELECT tags_id,photos_id FROM bridge WHERE bridge.tags_id='{$tag}' AND bridge.photos_id={$slika} ");
if($check->num_rows()==0){
$this->db->insert('bridge',$data);
$res = $this->db->query("SELECT name FROM tags where `tags`.`id`='{$tag}' ");
return $res;
}
}
you are passing variables correctly, but do not get them correctly in the model, which should look like this:
public function add_tags($slike, $tag){
//your other code
}
The following code write on the controller file:-
$data = array();
$this->load->model('dbmodel');
$data['item'] = $this->dbmodel->getData('*','catagory',array('cat_id'=>21));
$this->load->view('listing_view', $data);
The following code write on the dbmodel file:-
public function getData($cols, $table, $where=array()){
$this->db->select($cols);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;}

Joomla 2.5 pagination

I want to add pagination to my component, i've created a simple model with query. I must be missing something. What else do I need here ?
MODEL
jimport('joomla.application.component.modellist');
class PaieskaModelPradinis extends JModelList
{
public function getListQuery()
{
$db = JFactory::getDBO();
$query = "SELECT * FROM #__content";
$db->setQuery( $query );
$db->query( $query );
$result = $db->LoadObjectList();
return $result;
}
}
VIEW
jimport( 'joomla.application.component.view');
class PaieskaViewPradinis extends JView
{
protected $items;
protected $pagination;
function display ($tpl = null)
{
$this->items = $this->get('ListQuery');
$this->pagination = $this->get('Pagination');
parent::display($tpl);
}
}
TPL
foreach ($this->items as $item) {
echo $item->title;
}
EDITED:
I edited a bit code, so now it works fine, almost. Button display(number of rows to display) is not working. And I wonder if this part can be done in a different way ?
$limit = JRequest::getVar('limit' , 25);
$start = JRequest::getVar('start' , 0);
$query = "SELECT * FROM #__content LIMIT $start, $limit";
-
class PaieskaModelPradinis extends JModelList
{
public function getItems()
{
$db = JFactory::getDBO();
$limit = JRequest::getVar('limit' , 25);
$start = JRequest::getVar('start' , 0);
$query = "SELECT * FROM #__content LIMIT $start, $limit";
$db->setQuery( $query );
$db->query( $query );
$lists = $db->LoadObjectList();
return $lists;
}
function getPagination()
{
$main = JFactory::getApplication();
$db = JFactory::getDBO();
$limit = JRequest::getVar('limit' , 25);
$limitstart = JRequest::getVar('limitstart', 0);
$query = "SELECT count(title) FROM #__content";
$db->setQuery( $query );
$total = $db->loadResult();
// include a pagination library
jimport('joomla.html.pagination');
$pagination = new JPagination($total, $limitstart, $limit);
return $pagination;
}
}
VIEW
jimport( 'joomla.application.component.view');
class PaieskaViewPradinis extends JView
{
function display($tpl = null)
{
$this->items = $this->get('items');
$this->pagination = $this->get('pagination');
parent::display($tpl);
}
}
Going off your original code, not the edited version.
The getListQuery method only builds your database query, so you don't execute your query here. Use com_weblinks as an example for building out your model: https://github.com/joomla/joomla-cms/blob/2.5.x/components/com_weblinks/models/category.php
follow this link http://docs.joomla.org/J1.5:Using_JPagination_in_your_component properly.
I have used Joomla pagination. You'll be able to use Joomla pagination easily, if you follow documentation properly. BTW its very simple.

joomla loadformdata

how to show data from 3 tables in one view, because using JTable i can show data only bind to that JTable, please help me with this one.
my code so far(not working) in models:
public function getEntireProject(){
$item_id = $this->getItem()->id;
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__project_part_1 AS a');
$query->leftJoin('#__project_part_2 AS u ON a.uuid = u.uuid');
$query->leftJoin('#__project_part_3 AS y ON a.uuid = y.uuid');
$query->where('a.id = '. (int) $item_id);
$db->setQuery($query);
return $db->loadResult();
}
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_web_projects.edit.webproject.data', array());
if (empty($data)) {
$data = $this->getEntireProject();
}
return $data;
}
try to overwrite getItem function.This will also be helpful if you are calling get('Item') in view. -
public function getItem($pk = null){
if ($item = parent::getItem($pk)) {
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__project_part_1 AS a');
$query->leftJoin('#__project_part_2 AS u ON a.uuid = u.uuid');
$query->leftJoin('#__project_part_3 AS y ON a.uuid = y.uuid');
$query->where('a.id = '. (int) $item->id);
$db->setQuery($query);
$item = $db->loadAssoc();
}
return $item;
}
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_web_projects.edit.webproject.data', array());
if (empty($data)) {
$data = $this->getItem();
}
return $data;
}
For Multi-Row Results use loadRowList(), loadAssocList(), loadAssocList($key), loadObjectList(), loadObjectList('key'). $db->loadResult() only load one result. Read more.
If I understand your question right this should fix your problem. If you not please ask.

Resources