PHP Version 5.4.13 Bug? CodeIgniter Bug? Or, Explanation? - php

So... I pushed some code live the other day (that worked 100% fine on my local machine) but killed the server - no Codeigniter Logs, no Apache Logs, die('msg') and exit() did not work - I have never experienced this before in 5+ years of PHP development.
After 50+ commits to my repo I narrowed the problem down to one statement that works when split apart, but not together.
System Info:
PHP Version: 5.4.13
Codeigniter Version: define('CI_VERSION', '2.1.3');
These lines work (being called in a Codeigniter MY_Controller function):
dump($this->get_val('order_id'));
$tmp = $this->get_val('order_id');
dump($tmp);
dump(empty($tmp));
dump(!empty($tmp));
But when I add this following line the above described crash happens:
!empty($this->get_val('order_id'))
This seems like a PHP bug?
Structure:
Main.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Main extends Administration {
function index() {
// if (!empty($in['order_id'])) { /* BROKEN */
dump($this->get_val('order_id'));
$tmp = $this->get_val('order_id');
dump($tmp);
dump(empty($tmp));
dump(!empty($tmp));
// dump(!empty($this->get_val('order_id'))); /* BROKEN */
// if (!empty($this->get_val('order_id'))) { /* BROKEN */
// dump(true);
// } else {
// dump(false);
// }
}
}
adminstration.php
<?php
class Administration {
/**
*
* #var MY_Controller;
*/
public $ci;
public $p;
function __construct() {
$this->ci = & get_instance();
$this->ci->load->model('user/admin/user_admin_model');
$this->p = $this->ci->uri->uri_to_assoc(4);
}
protected function get_val($name = '') {
$pst = (array) $this->ci->input->post();
$gt = (array) $this->ci->input->get();
if (empty($name)) {
return array_merge($pst, $gt, $this->p);
}
if (!empty($this->p[$name]))
return $this->p[$name];
if (!empty($pst[$name]))
return $pst[$name];
if (!empty($gt[$name]))
return $gt[$name];
return array();
}
}
?>
My_Controller.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $p;
function __construct() {
parent::__construct();
$this->p = $this->uri->uri_to_assoc();
}
function get_val($name = '') {
dump("I am get_val in MY_controller");
$pst = (array) $this->input->post();
$gt = (array) $this->input->get();
if (empty($name)) {
return array_merge($pst, $gt, $this->p);
}
if (!empty($this->p[$name]))
return $this->p[$name];
if (!empty($pst[$name]))
return $pst[$name];
if (!empty($gt[$name]))
return $gt[$name];
return array();
}
}

Prior to PHP version 5.5.0, empty only worked on variables, not on the returned value from a function or directly on the result of an expression

Related

CodeIgniter: Class constructer activated on library load?

When I load a library in CI controller the class constructor is activated automatically, even if I didn't create an object just yet.
This is weird. can this be fixed via config or something? didn't find anything on the web.
My Controller:
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('users',false);
$user = new Users();
$this->load->model('welcome_model');
$this->load->view('welcome_message');
}
}
My Class:
defined('BASEPATH') OR exit('No direct script access allowed');
class Users {
public function __construct($uname, $fname, $lname) {
print "Hello World!";
}
public function some_method() {
}
}
The code will output "Hello World" twice.
$this->load->library('myLib'); instanciate the library, so you don't need to use the new keyword.
$params_for_the_constructor = array(
'id' => 1,
'firstname' => 'Jemmy',
'lastname' => 'Neutron'
);
$this->load->library('users', $params_for_the_constructor);
$this->users->some_method();
class Users {
private $id;
private $firstname;
private $lastname;
public function __construct(array $params = array()) {
if (count($params) > 0) {
$this->initialize($params);
}
log_message('debug', "Users Class Initialized");
}
public function some_method() {
}
public function initialize(array $params = array()) {
if (count($params) > 0) {
foreach($params as $key => $val) {
if (isset($this->{$key}))
$this->{$key} = $val;
}
}
return $this;
}
}
https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
in Codeigniter when you call library or call helper or call a controller or a model or anything of these its just equivalent to the new key word.
i mean if you want to make a new object of user you just do the following.
just store the instance of your first user in a varaible.
//creating first instance
$user = $this->load->library('users', $first_user_parameters);
//creating second instance
$user2 = $this->load->library('users', $second_user_parameters);
// $this->load->library('users', $param) === $user = new User($param);
in codeigniter they convert the load to new keyword by the ci_loader

My CodeIgniter project does not work in server

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
function __costruct(){
parent::__costruct();
$login = $this->session->userdata('login');
if(!empty($login)){
if($login!='valid'){
} else {
redirect('login/index');
}
} else{
redirect('login/index');
}
}
protected $template = array();
public function layout($arg = array()) {
$this->template['header'] = $this->load->view('theme/header_theme', $arg, true);
$this->template['header_menu'] = $this->load->view('theme/header_menu_theme', $arg, true);
$this->template['sidebar'] = $this->load->view('theme/sidebar_theme',$arg, true);
$this->template['content'] = $this->load->view($this->content, $arg, true);
$this->template['footer'] = $this->load->view('theme/footer_theme',$arg, true);
$this->load->view('theme/index_theme', $this->template);
}
If I am reading it right, and if that is the code used, the mistake is a simple spelling mistake.
function __costruct(){
parent::__costruct();
should have been
function __construct(){
parent::__construct();
Anyways, it is always better to use the server in development mode to check the same!!!

How do i properly create a function in a helper file in codeigniter

I am trying to learn code igniter and creating helper files. I have this as my functions_helper.php file located in my applications/helpers folder:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
if (!function_exists('check_status')){
function check_status()
{
$CI = & get_instance();
$result=$CI->user->get_status(); //this is line 14!!
if($result)
{
$status_array=array();
foreach($result as $row)
{
$status_array=array(
'source' => $row->status,
'current' => $row->id
);
if ($status_array['current'] == 1) {
$status_array['current'] = "Live";
} else {
$status_array['current'] = "Amazon is Down";
}
$CI->session->set_userdata('status',$status_array);
}
return TRUE;
}
else
{
return false;
}
} // END OF CHECK_STATUS FUNCTION
}
From everything i can find, I am doing it correctly, yet I am getting this error:
PHP Fatal error: Call to a member function get_status() on a non-object in function_helper.php on line 14. exactly what am i doing wrong? Is this the best way to call a function? Ultimately I am trying to get information returned from a db query.
My get_status function is:
//FUNCTION TO SEE THE STATUS OF AMAZON
function get_status() {
$query = $this->db->query("select * from amz where active = 1");
if($query->num_rows()==1) {
return $query->row_array();
}else {
return false;
}
} //END OF GET_STATUS FUNCTION
Remove this code
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
There is no OO in helper files. You do not have a class -__construct() are used to initialize classes.
Then move
$this->load->model('user','',TRUE); to the check_status function
$CI =& get_instance();
$CI->load->model('user','','TRUE');

Codeigniter: Load model and controller result in Helper

No idea what is the right way. But what I am trying to do is to make one helper function for positions list or dropdown (will set parameter for this) so that's how position list/dropdown can available to the entire system by writing single code `get_positions($type)'
The problem is not loading Model/Controller variables.
Error message:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: positions
Filename: helpers/content_helper.php
Line Number: 14
Helper
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// Position List
if ( ! function_exists('get_positions'))
{
function get_positions()
{
$CI =& get_instance();
$CI->load->model('admin/hr/hr_model');
if(count($positions)):
echo form_open();
$array = array();
foreach($positions as $position):
$label[] = $position->label;
$pos[] = $position->position;
endforeach;
echo form_dropdown('positions', array_combine($pos, $label));
echo form_close();
endif;
}
}
Model
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class HR_Model extends MY_Model
{
protected $_table_name = 'positions';
protected $_order_by = 'label ASC';
public $rules = array();
public function get_new()
{
$position = new stdClass();
$position->position = '';
$position->label = '';
return $position;
}
public function get_positions($id = NULL, $single = FALSE)
{
$this->db->get($this->_table_name);
return parent::get($id, $single);
}
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class HR extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('admin/hr/hr_model');
}
public function index()
{
// Fetch all pages
$this->data['positions'] = $this->hr_model->get_positions();
// Load view
$this->load->view('hr/index', $this->data);
}
}
Eventually what I am looking is to create helper function which available to entire system by calling get_positions($type)
You have to define the $positions variable before if (count($positions)):. You should call the get_positions method from the Model.

stored procedure with multiple resultset in Codeigniter

I have been working on AMFPHP + codeigniter + flash and everything was working just fine but, when I created a stored procedure the problem started. I was able to call the stored procedure with multiple result sets from the AMF browser but whenever the function was called from the flash itself it raises the Bad Version error.
Below is the library {from CI forum} to traverse multiple result sets am using
class Mydb
{
private $CI, $Data, $mysqli, $ResultSet;
/**
* The constructor
*/
function __construct()
{
$this->CI =& get_instance();
$this->Data = '';
$this->ResultSet = array();
$this->mysqli = $this->CI->db->conn_id;
}
public function GetMultiResults($SqlCommand)
{
/* execute multi query */
if (mysqli_multi_query($this->mysqli, $SqlCommand)) {
$i=0;
do
{
if ($result = $this->mysqli->store_result())
{
while ($row = $result->fetch_assoc())
{
$this->Data[$i][] = $row;
}
mysqli_free_result($result);
}
$i++;
}
while ($this->mysqli->next_result());
}
return $this->Data;
}
}
and calling it like this:
$this->load->library('mydb');
$this->mydb->GetMultiResults("CALL test()");
I have noticed that the library loading line raises the Bad Version error at flash end as if I comment out this line it works {works like no error is ther but SP doesn't execute}
Any Idea on how to fix this strange issue.
Although CodeIgniter uses mysqli driver the next_result() method is not set in the DB_driver.
Here and here you can find further details. The idea is:
define next_result() method in system/database/DB_driver.php:
function next_result()
{
if (is_object($this->conn_id))
{
return mysqli_next_result($this->conn_id);
}
}
(be sure to push this up to your notes pile for the CI system core upgrade )
make sure you are using mysqli drive (application/config/database.php)
In your model after you call your stored procedure and BETWEEN each fetch of results use $query_or_somethig-else->next_result(); (and use $query_or_somethig-else->free_result(); in the final)
I have been working on multiple query execution in store procedure in codeigniter
I created a library named SP.php. Here is the code also change in config to database =
mysqli
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class CI_SP {
private $CI;
function __construct() {
$this->CI = & get_instance();
}
public function GetResults($SqlCommand) {
$k = 0;
$arr_results_sets = array();
/* execute multi query */
if (mysqli_multi_query($this->CI->db->conn_id, $SqlCommand)) {
do {
$result = mysqli_store_result($this->CI->db->conn_id);
if ($result) {
$l = 0;
while ($row = $result->fetch_assoc()) {
$arr_results_sets[$k][$l] = $row;
$l++;
}
}
$k++;
} while (mysqli_next_result($this->CI->db->conn_id));
return $arr_results_sets;
}
}
}
and the model code is
$result = $this->sp->GetResults("call function(parameters);
$result1 = $this->sp->GetResults("call function(parameters));
print_r($result);
print_r($result1);
exit;
return $query->result_array();

Categories