Loading model prints query - php

I'm trying to setup a codeigniter-page which already works perfect on another sever.
I have changed the database.php file such that it connects to the local database instead. Else, nothing else is changed.
But when i load the page it just print out the queries from my model.
I believed I'm well connected to the database I experience other errors if there is no connection to the DB.
I believe it is a configuration error as nothing else is changed on the page (and it is working fine on another server).
There is used CodeIgniter 2.1.3
Does any know this error?
UPDATE: Here is snips of my controller (I call index-page). The inherited class (MY_Controller) is empty
class Page extends CI_Controller {
public function __construct() {
// session_start();
parent::__construct();
$this->load->model('Page_model');
}
public function index() {
// $this->show(1);
}
And the page_model which i load:
<?
class Page_model extends CI_Model {
function __construct(){
parent::__construct();
}
function get_page($id){
$this->db->where('id', $id);
$query = $this->db->get('gahk_page');
return $query->result();
}
function update_by_id($id, $data){
$this->db->where('id', $id);
$this->db->update('gahk_page', $data);
}
}
?>

Your server may not be supporting PHP "short_open_tag", so the actual code is displayed in model.
Enable "short_open_tag" in php.ini or use <?php ?> tags in models.

Related

Why does my PHP error controller work with PHP 5, but not PHP 7?

I'm migrating my project from PHP 5 to PHP 7.3, I have made the changes for the decrypted function with alternative functions. But am facing issue with the one controller file.
The same code works for PHP5 version, But when am trying to execute same code for PHP 7 it doesn't give any error even there is no error got added in errorLog file. Could you please help me out this.
I'm uploading my 'error.php' Controller file.
<?php
class Error extends CI_Controller {
private $controller = "error";
public function __construct() {
parent::__construct();
if ($this->phpsession->get('USERID')) {
$headerContent['controller'] = $this->controller;
$this->load->view('xome/header', $headerContent);
} else {
header("Location:" . ASITEURL . "/login/");
}
}
public function index() {
$this->load->view('x-404');
$this->load->view('xome/footer');
}
public function permission() {
$this->load->view('x-permission');
$this->load->view('xome/footer');
}
public function display() {
$this->load->view('x-error');
$this->load->view('xome/footer');
}
}
?>
When I hit the URL it should load the view page but unable to load any view file.
http://localhost/--project folder name--/error/permission
Even I checked there is no syntax error in the controller as well as any view file.
As of PHP7, Error is a reserved class name: http://php.net/manual/en/class.error.php.
Change it to something else:
class MyError extends CI_Controller
{
// ....
}

How to fix Codeigniter error: Call to a member function on null?

It's my first time exploring codeigniter and I've been trying to create a login where if users are not found in the database, it will redirect back to the login screen. I have not been successful because I've been getting this error:
first error message
Followed by
second error message
I've tried loading the database and session inside function verify() using
$this->load->database();
$this->load->session();
but even so, I didn't think it would work since I already declared database and session in autoload.php
$autoload['libraries'] = array('database', 'session');
Here's the controllers/Login.php
class Login extends CI_Controller{
function index(){
$this->load->view("templates/_header");
$this->load->view("login");
$this->load->view("templates/_footer");
$this->load->view("templates/_scripts");
}
function verify(){
$this->load->model('user');
$check = $this->model->validate();
if($check){
} else {
redirect('login');
}
}
}
Here's the models/User.php
<?php
class User extends CI_Model{
function validate(){
$arr['username'] = $this->input->post('username');
$arr['password'] = password_hash($this->input->post('password', PASSWORD_DEFAULT));
return $this->db->get_where('users', $arr)->row();
}
}
What do you guys think is the problem with it?
you have load your model true way but to use it you have to call its class name (here is User) to work work correctly.
note: if you want to code in a clean way and don't conflict them try to name your models like User_model or User_m, so if you have a controller with name of User its not conflict with your model.
:-)

can't access codeigniter controller function from curl but in browser working

I provide the API in my website so need to access the site from curl. when call the particular url from curl it can't be accessed. but in the browser it shows the result. If we put echo to check the process, upto parent construct we can get the result
class Apicall extends CI_Controller {
// Constructor function
public function __construct()
{
echo "test"; //this echo worked
parent::__construct();
echo "hfksdjhfkjsdhflksdhflkjh"; //this is not worked
$this->load->library(array('form_validation'));
$this->load->helper(array('url', 'language'));
}
public function v1()
{
}
}
The problem is in our core controller we write the redirect function so now its working fine

Where should I define a custom function in CodeIgniter and how can I use it in every page?

I'm a newbie in CodeIgniter. I want to use the following function and needs to be run in every page. I'm calling the function in the top header of the view. Even though different subviews are loaded in the content below, the navigation bar should load this function in order to count notifications and messages on every run.
function count_records_in_table($table, $where)
{
$this->db->select('count(id) as rows');
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
foreach($query->result() as $r)
{
return $r->rows;
}
}
first create the common model
then put this function on it and load this model in
$autoload['model'] = array('mdl_common');
autoload file in config
finally you can use your function by calling model common like this
$this->mdl_common->count_records_in_table("table_name","where");
you can use in every page you want try it it will helps .
Here is suitable solution
First create one file say common.php in application/libraries/common.php
Now write below code in common.php file
class Common extends CI_Controller
{
public function customFailMessage($msg)
{
$xml = new SimpleXMLElement('<Response/>');
$xml->addChild('status','success');
$xml->addChild('msg',"my message");
print ($xml->asXML());
die;
}
}
Now call you function from any of your controller like this
class Ws_plan extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('common');
}
public function test()
{
$this->common->customFailMessage();
}
}
Hope you got idea how to use common function in all controller

Codeigniter extends core class

I want to have class that checks login on all controllers that I specified.
Codeigniter version is 2.1.0 and I have php 5.3.10
Hier is how I would set it up:
I look at https://www.codeigniter.com/user_guide/general/core_classes.html
and I set it up like this:
in the /application/core/MY_Main.php
class MY_Main extends CI_Controller {
function __construct()
{
parent::__construct();
}
}
In my controller I have welcome.php
?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends MY_Main {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('welcome_message');
}
}
So if I place login check in MY-Main it should get work, but I cant get it work anyone???
I needed to add the following code to /application/config/config.php before I got the extenson of core classes working as described in the CI manual.
Code taken from here http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
#include_once( APPPATH . 'core/'. $class . EXT );
}
}
You logic is correct, that should work. It's exactly what I do on all my codeigniter sites. My code is a bit more complex as my login check is being called from a library (so I have to call $CI =& get_instance(); and then $CI in place of $this) but something like below should work for you. logged_in is just a name given to an item of session data set when the user logs in.
class MY_Main extends CI_Controller {
function __construct()
{
parent::__construct();
$session_data = $this->session->all_userdata();
if(!isset($session_data['logged_in']))
redirect('/login');
}
}
In regards to your comment above (http 500), not really sure what's going on there. The code you have pasted shouldnt be throwing errors like that so something else is probably going on. Try turning on codeigniters built in logging functionality.
http://codeigniter.com/user_guide/general/errors.html
You should create a library class and put it inside your library folder and load it as auto_load or inside your controllers.
create functions inside your library for example:
/**
*
* #return boolean check if a user is logged in or not
*/
function notLogin()
{
if (!$this->is_logged_in()){
//echo "pelase <a href='login'><b>login</b></a> to continue ";
redirect('home/login','refresh'); exit;
}
return true;
}
and call it inside your controller constructor or any functions you want like this:
class Main extends CI_Controller
{
private $POST = array();
private $ci_form;
function __construct()
{
parent::__construct();
//check if user is logged in or not
$this->m_auth->notLogin();
$this->load->library('form_validation');
$this->load->library('ajax_pagination');
}
}
It some time happens because of database connection.
Please check if your database :
has been selected by turning-on error reporting from your Cpanel error log.
user has been added to your database.

Categories