Call to undefined method CI_DB_mysqli_driver::groupBy() - php

I've created a web app using code igniter 3 to get data from 3 tables and display them in the view (quiz_table,question_table and answers_table).
Below is the code in my controller,
public function loadSingleQuizData_get()
{
$quizId = $this->uri->segment(3);
$this->load->model('QuizModel');
$singleQuizQuestionData = $this->QuizModel->getSingleQuizQuestionDataFromDB($quizId);
$data = array('singleQuizQuestionData' => $singleQuizQuestionData);
print json_encode($data);
}
and below is the code in the model
function getSingleQuizQuestionDataFromDB($quizId)
{ //insert query
try {
$this->db->select('quiz_table.quizName');
$this->db->select('quiz_table.creatorName');
$this->db->select('quiz_table.rating');
$this->db->select('question_table.questionId');
$this->db->select('question_table.questionTitle');
$this->db->select('question_table.correctAnswer');
$this->db->select('answer_table.answer');
$this->db->from('quiz_table');
$this->db->where('quiz_table.quizId',$quizId);
$this->db->join('question_table','question_table.quizId = quiz_table.quizId','INNER');
$this->db->join('answer_table','answer_table.questionId= question_table.questionId','INNER');
$this->db->from('quiz_table');
$this->db->groupBy(['quiz_table.quizId', 'question_table.questionId']);
$result = $this->db->get();
$singleQuizQuestionData= $result->result_array();
return $singleQuizQuestionData;
} catch (Exception $e) {
// log_message('error: ',$e->getMessage());
return;
}
}
When I try to load the results in the view I get the below error message
Please help!

https://codeigniter.com/userguide3/database/query_builder.html
The syntax for group by in codeigniter is group_by not groupBy (that would be laravel)

Related

Not unique table/alias: 'quiz_table'

I've created a web app using code igniter 3 to get data from 3 tables and display them in the view (quiz_table,question_table and answers_table).
Below is the model code,
function getSingleQuizQuestionDataFromDB($quizId)
{ //insert query
try {
$this->db->select('quiz_table.quizName');
$this->db->select('quiz_table.creatorName');
$this->db->select('quiz_table.rating');
$this->db->select('question_table.questionId');
$this->db->select('question_table.questionTitle');
$this->db->select('question_table.correctAnswer');
$this->db->select('answer_table.answer');
$this->db->from('quiz_table');
$this->db->where('quiz_table.quizId',$quizId);
$this->db->join('question_table','question_table.quizId = quiz_table.quizId','INNER');
$this->db->join('answer_table','answer_table.questionId= question_table.questionId','INNER');
$this->db->from('quiz_table');
$this->db->group_by(['quiz_table.quizId', 'question_table.questionId']);
$result = $this->db->get();
$singleQuizQuestionData= $result->result_array();
return $singleQuizQuestionData;
} catch (Exception $e) {
// log_message('error: ',$e->getMessage());
return;
}
}
When I try to load the result I get the below error
Please help!
Using multiple from() clauses are not allowed in Codeigniter. Removing the second from() will fix the issue
$this->db->from('quiz_table'); <---- here
$this->db->where('quiz_table.quizId',$quizId);
$this->db->join('question_table','question_table.quizId = quiz_table.quizId','INNER');
$this->db->join('answer_table','answer_table.questionId= question_table.questionId','INNER');
$this->db->from('quiz_table'); <---- here
$this->db->group_by(['quiz_table.quizId', 'question_table.questionId']);

How to get users list in codeigniter?

I am trying to get list of users from Users table in codeigniter. My problem is that if am using following code than try to print out the result, it's giving me blank array while printing query using last_query() function and running this query in phpmyadmin is giving correct result.
My code in model
$data = $this->db->select('u.*')
->from('users u')
->join('users_groups ug','ug.user_id = u.id')
->join('groups g','g.id = ug.group_id')
->where('g.id',7)
->get()
->result();
this code is giving me blank array then what i did is print the query using following code
echo $this->db->last_query();
and this is what query was
SELECT `u`.* FROM `users` `u` JOIN `users_groups` `ug` ON `ug`.`user_id` = `u`.`id` JOIN `groups` `g` ON `g`.`id` = `ug`.`group_id` WHERE `g`.`id` = 7
which is working fine. but i am trying to print the data using print_r($data)
is giving me blank array.
Can anyone tell me what can be the issue ?
edited
My full model code.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Technician_model extends CI_Model {
public function _consruct(){
parent::_construct();
}
function getData(){
$data = $this->db->select('u.*')
->from('users u')
->join('users_groups ug','ug.user_id = u.id')
->join('groups g','g.id = ug.group_id')
->where('g.id',7)
->get()
->result();
return $data;
}
function getRecord($user_id){
$record = $this->db->get_where('users',array('id'=>$user_id))->row();
echo '<pre>';
print_r($record);
exit;
return $record;
}
function addModel($data){
if($this->db->insert('users',$data)){
return $this->db->insert_id();
}
else{
return FALSE;
}
}
function editModel($data,$user_id){
$this->db->where('id',$user_id);
if($this->db->update('users',$data)){
return true;
}
else{
return false;
}
}
function deleteModel($user_id){
$this->db->where('id', $user_id);
if($this->db->delete('users')){
return true;
}
else{
return FALSE;
}
}
}
?>
You may try this simple way to join table. you may try this and can
change fields name table name and method according to your
require.
public function getUser() {
$this->db->select('users');
$this->db->from('Users');
$this->db->where('users_groups.user_id',1);
$this->db->join('users_groups', 'users.id = users_groups.user_id');
$query = $this->db->get();
$result = $query->result_array();
var_dump($result); // display data in array();
return $result;
}
Actually, at the end of 2 hrs of the journey, I found that I forgot to configure new database.
All the answers were correct in this case.

i want to display value from database in codeigniter but i am getting errror

I want to display a value in view in CodeIgniter but I am getting several errors like trying to get the property on non-object. I think my code is correct but I am getting errors. Below is my code.
controller:
public function trainer($id)
{
$user_record = $this->db->query("select * from usr_data where usr_id=$id")->result();
$data['title'] = 'trainer Dashboard';
$data['user_record'] = null;
$data['active_courses'] = [];
$data['inprogress'] = [];
if(count($user_record)) {
$user_record = $user_record[0];
$data['title'] = ucwords($user_record->firstname).' Dashboard';
$data['user_record'] = $user_record;
$active_courses = $this->base_model->getTrainercourseAll($id);
$data['active_courses'] = $active_courses;
$inprogress = $this->base_model->getstaffinprogress($id);
$data['inprogress'] = $inprogress;
}
$this->load->view('trainer-dashboard', $data);
}
model:
public function getstaffinprogress($user_id) {
$result=$this->executeSelectQuery("select AVG(m.percentage) from object_data o, ut_lp_marks m where o.obj_id=m.obj_id and o.type='crs' and m.status=1 ");
return $result;
}
view:
<h3>Avg inprogress:<?php echo "<span style='color:#ff00ff;font-family:verdana;'>".$inprogress->percentage."</span>";?></h3>
I want to display the column percentage which is coming from database.above code is in the controller, model and view.i thought my controller code is wrong.
Anyone help me to get rid of this error. I want to display a value in view in CodeIgniter but I am getting several errors like trying to get the property on non-object. I think my code is correct but I am getting errors. Below is my code.
Try this in your view file,
if(isset($inprogress)){
echo $inprogress->percentage;
}
Then your code look like this,
<h3>Avg inprogress:<?php if(isset($inprogress)){ echo "<span style='color:#ff00ff;font-family:verdana;'>".$inprogress->percentage."</span>";}?></h3>
Then call the controller function. I think inprogress is not set at the first time.
If it doesn't work, try to var_dump($inprogress) in controller and check value and type.
And try this code in your model. Query also seems not correct
public function getstaffinprogress($user_id) {
$this->db->select_avg('ut_lp_marks.percentage');
$this->db->where('ut_lp_marks.obj_id', $user_id);
$this->db->where('object_data.type', 'crs');
$this->db->where('ut_lp_marks.status', 1);
$this->db->join('object_data', 'object_data.obj_id = ut_lp_marks.obj_id');
$query = $this->db->get('ut_lp_marks');
return $query->result_array();
}
I assume that your db is ut_lp_marks. Then var_dump array and check data is correct first. Then access array element.
public function getstaffinprogress($user_id) {
$result = array();
$query=$this->db->query("select AVG(m.percentage) from object_data o, ut_lp_marks m where o.obj_id=m.obj_id and o.type='crs' and m.status=1 ");
foreach($query->result() as $row){
$result = $row;
}
return $result;
}
Also check $inprogress->percentage exists before print in view.

When I update my data with PHP CodeIgniter, It gives an error. But data is saving

I am using 'CodeIgniter-3.1.3'. When I update my data, it gives an error as follow. But my data is saving in database. I can't find what is the issue. Is there any issue with my model function related to version..?
Error : Fatal error: Call to undefined method CI_DB_mysqli_driver::_error_number()
In Controller
public function saveUpdateData(){
$updateDataArray = array(
"fname" => $_POST['fname'],
"lname" => $_POST['lname'],
"username" => $_POST['user'],
"userlevel" => $_POST['userlevel'],
"contact" => $_POST['contact_no']
);
$whereArr=array("user_id"=>$this->input->post("user_id"));
$rst = $this->MyModel->updateData("admin", $updateDataArray, $whereArr);
if ($rst) {
echo "updated";
}
else{
echo "error";
}
}
In model
function updateData($tablename, $data_arr, $where_arr) {
try {
$this->db->update($tablename, $data_arr, $where_arr);
$report = array();
$report['error'] = $this->db->_error_number();
$report['message'] = $this->db->_error_message();
return $report;
} catch (Exception $err) {
return $err->getMessage();
}
}
enter image description here
Do like this
In Model
function updateData($tablename, $data_arr, $where_arr)
{
if (!$this->db->update($tablename, $data_arr, $where_arr)) {
$result = $this->db->error();
}
else {
return TRUE;
}
}
In Controller
$rst = $this->MyModel->updateData("admin", $updateDataArray, $whereArr);
if ($rst == TRUE) {
echo "updated";
}
else{
print_r($rst);
}
Read Error Handling Codeigniter
In CodeIgniter 3+, you can get errors with error no and messages as by using the following:
$error = $this->db->error();
Reference: Handling Query Errors in CodeIgniter
The functions you are trying to access have been deprecated.
Replace:
$this->db->_error_number()
$this->db->_error_message()
With:
$this->db->error()
The new function will return an array with both the error number and message.
Refer to this CodeIgniter page:
https://www.codeigniter.com/userguide3/changelog.html?highlight=_error_number

REST API with CodeIgniter

I am creating a web-service backend for a mobile app I am developing. (I am an experience Obj-C developer, not a web-designer!) Essentially I would like to use Codeigniter and Phil Sturgeon's RESTful API Server https://github.com/philsturgeon/codeigniter-restserver however, I'm having some trouble getting it all setup and working.
I have MySQL database that is setup with data in it. And I need some help writing a CodeIgniter PHP model and controller that returns JSON data of what is inside that database. All of the tutorials and forum post's i have found deal with hardcoded data in the controler, not a MySQL database. I would ideally like to have the URL formatted like this http://api.mysite.com/v1/search?id=1&name=foo&city=bar , I could potentially have 50+ parameters to pass in the url.
Using Phil's code, I have come up with this as my controller:
public function index_get()
{
if (!$this->get('id'))
{
$this->response(NULL, 400);
}
$data = $this->grid_m->get_id($this->get('id'));
if ($data)
{
$this->response($data, 200);
}
else
{
$this->response(NULL, 404);
}
}
That only gets me one search term: id?=# .. I need to know how to get multiple search terms
Here is my Codeigniter model:
<?php
class Grid_m extends CI_Model
{
function get_all()
{
$query = $this->db->get('grid');
if ($query->num_rows() > 0)
{
return $query->result();
}
return FALSE;;
}
This just returns EVERYTHING in my MySQL database regardless of what id or url term I pass it in the URL.
I'm a big noob when it comes to developing my own custom API so any suggestions on how to fix my controller and database model would be a huge help!
Thanks for the help!
-brian
This is old question but if somebody go here and still need help, try these code:
In your controller:
public function index_get()
{
$where = '';
if ($this->get('id')) // search by id when id passed by
{
$where .= 'id = '.$this->get('id');
}
if ($this->get('name')) // add search by name when name passed by
{
$where .= (empty($where)? '' : ' or ')."name like '%".$this->get('name')."%'";
}
if ($this->get('city')) // add search by city when city passed by
{
$where .= (empty($where)? '' : ' or ')."city like '%".$this->get('city')."%'";
}
// you can add as many as search terms
if ( ! empty($where))
{
$data = $this->grid_m->get_searched($where);
if ($data)
{
$this->response($data, 200);
}
else
{
$this->response(NULL, 404);
}
}
else
{
$this->response(NULL, 404);
}
}
In your model, create get_searched function:
class Grid_m extends CI_Model
{
function get_searched($where = NULL)
{
if ( ! is_null($where))
{
$this->db->where($where);
$query = $this->db->get('grid');
if ($query->num_rows() > 0)
{
return $query->result();
}
}
return FALSE;
}
}
User Codeigniter's Active record to build proper query, you can build any type of query using methods of active record, refer following example I have just added one condition in it you can add more conditions as per your need.
<?php
class Grid_m extends CI_Model
{
function get_all()
{
$this->db->select('col1, col2, col3')->where('id', 5);
$query = $this->db->get('grid');
if ($query->num_rows() > 0)
{
return $query->result();
}
return FALSE;;
}
}
Please check you query
$query = $this->db->get_where('grid', array('id' => $id));

Categories