How to print SQL statement in codeigniter model
To display the query string:
print_r($this->db->last_query());
To display the query result:
print_r($query);
The Profiler Class will display benchmark results, queries you have run, and $_POST data at the bottom of your pages.
To enable the profiler place the following line anywhere within your Controller methods:
$this->output->enable_profiler(TRUE);
Profiling user guide:
https://www.codeigniter.com/user_guide/general/profiling.html
Echo query before execution and without execution in codeigniter Active Record
You can see the compiled query by either of these functions
/* SELECT */ $this->db->_compile_select();
/* INSERT */ $this->db->_insert();
/* UPDATE */ $this->db->_update();
Echo/Print Query Result at Controller in Codeigniter
You have multi-dimentinal array returning from model. So you have to loop over them then print data
function updateJobsheetCDC()
{
$prnID = $this->input->post('prnID');
$this->load->model('Ipss_model');
$data = $this->Ipss_model->prnEmailList($prnID);
foreach ($data as $key => $value) {
echo $value->name;
}
}
Codeigniter: does $this-db-last_query(); execute a query?
The query execution happens on all get methods like
$this->db->get('table_name');
$this->db->get_where('table_name',$array);
While last_query contains the last query which was run
$this->db->last_query();
If you want to get query string without execution you will have to do this.
Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions
public function _compile_select($select_override = FALSE)
public function _reset_select()
Now you can write query and get it in a variable
$this->db->select('trans_id');
$this->db->from('myTable');
$this->db->where('code','B');
$subQuery = $this->db->_compile_select();
Now reset query so if you want to write another query the object will be cleared.
$this->db->_reset_select();
And the thing is done. Cheers!!!
Note : While using this way you must use
$this->db->from('myTable')
instead of
$this->db->get('myTable')
which runs the query.
Take a look at this example
Print query before inserting (NOT EXECUTING) in Codeigniter Framework
please use insert_string, please take a look at following example.
$data = array(
'name' = > $_POST['name'] ,
'groupname'= > $_POST['groupname'],
'age' = > $_POST['age']
);
$this-> db->insert_string('tbl_user', $data);
Refer to this link
print database data with model function codeigniter
Model Code
class User1_model extends CI_Model
{
public function getUser($name)
{
$query = "SELECT * FROM `user`";
$data = $this->db->->query($query);
return $data->result_array();//for single user for all return result_array() and comment $this->db->where line
}
}
Controller Code
class User1 extends CI_Controller
{
public function index()
{
$this->load->model("user1_model");
$data['name'] = $this->user1_model->getUser("RANDIKA");
if($data['name'] == false )
$data['name'] = array();
$this->load->view('users', $data);
}
}
And in View
<?php print_r($name); // will print single users details in your case its RANDIKA ?>
Output the results of the SQL query in CodeIgniter is not displaying
There are a few problems with your code.
Firstly, instead of accessing the session directly in the model. You should access it in the controller, and pass the data to the model as a parameter of the getuser_id() function.
Secondly, you are making a query but not returning the data from it. To do this you will need to amend the line:
$query = $this->db->get();
To be one of the following, depending on your preference:
$query = $this->db->get()->result(); //returns results as an object
$query = $this->db->get()->result_array(); // returns results as an array
More info on returning results here.
http://ellislab.com/codeigniter/user-guide/database/results.html
Thirdly, you dont pass any data from your model back to your controller. You need to add a return $your_data
to the end of the models function.
Updated Model:
public function getuser_id($user_email)
{
$this->db->select('id')->from('users')->where('email', $user_email);
$query = $this->db->get()->result_array();
return $query; // Don't forget to return your results!
}
Here's the controller:
public function members()
{
if ($this->session->userdata('is_logged_in'))
{
$data['title'] = 'Members Page';
$this->load->model('model_users');
$user_email = $this->session->userdata('email');
$data['uid'] = $this->model_users->getuser_id($user_email);
$this->load->view('members', $data);
}
else
{
redirect('main/restricted');
}
}
Related Topics
How to Dynamically Populate Options on Dropdown Lists Based on Selection in Another Dropdown
PHP Composer Update "Cannot Allocate Memory" Error (Using Laravel 4)
Laravel Named Route for Resource Controller
Codeigniter Activerecord, Retrieve Last Insert Id
How to Echo the Whole Content of a .HTML File in PHP
Make Switch Use === Comparison Not == Comparison in PHP
How to Change Variables in the .Env File Dynamically in Laravel
Checking If Process Still Running
Get Variable from PHP File Using Jquery/Ajax
Error: MySQLnd Cannot Connect to MySQL 4.1+ Using the Old Insecure Authentication
How to Do Multiple SQL Statements in One MySQL_Query
Dynamically Generating a Qr Code with PHP
Is "Set Character Set Utf8" Necessary
Why, Fatal Error: Class 'Phpunit_Framework_Testcase' Not Found in ...
Stop Script Execution Upon Notice/Warning
PHP Simple Foreach Loop with HTML
Google Calendar API V3 - Authenticate with Hardcoded Credentials