0
votes

we are facing 1064 database error in google chrome browser sometimes. but its working fine in other browsers. please let me know any solutions you have.

Error Code:

A Database Error Occurred Error Number: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 select * from tbl_user_rights where usr_id=**

Filename: helpers/common_helper.php Line Number: 42**

program code:-

function user_has_right($rights_id) {
    $CI = get_instance();
    $CI->load->model('user_model');
    $user_id = $CI->session->userdata('user_id');
    $query = $CI->db->query("select * from " . TBL_USERS_RIGHTS . " where usr_id=" . $user_id);
    $rights = $query->result();
    $user_right = array();
    foreach ($rights as $right) {
        array_push($user_right, $right->sm_id);
    }// print_r($user_right);exit;
    if (in_array($rights_id, $user_right)) {
        return TRUE;
    } else {
        return FALSE;
    }
}
3
fire the select query only when $user_id is not empty - Bits Please

3 Answers

0
votes

You need to add condition for either user id exist or not. Try to put condition as in below code. Hope this can help you to solve this issue.

function user_has_right($rights_id) {
$CI = get_instance();
if($CI->session->userdata('user_id') != '')
{
    $CI->load->model('user_model');
    $user_id = $CI->session->userdata('user_id');
    $query = $CI->db->query("select * from " . TBL_USERS_RIGHTS . " where usr_id=" . $user_id);
    $rights = $query->result();
    $user_right = array();
    foreach ($rights as $right) {
        array_push($user_right, $right->sm_id);
    }// print_r($user_right);exit;
    if (in_array($rights_id, $user_right)) {
        return TRUE;
    } else {
        return FALSE;
    }
 }
 else
 {
      return FALSE;
 }
}
0
votes

Please add

$user_id = $CI->session->userdata('user_id');
$user_id = ($user_id && is_numeric($user_id)) ? $user_id : 0;

It will set user_id as zero if it not getting any thing

0
votes

The query is failing because the user_id is empty from the session. I would suggest returning false if the user_id is empty (by checking if it is null) and I even wrote a query builder get that has the benefits of escaping the user_id:

function user_has_right($rights_id) {
    $CI = get_instance();
    $CI->load->model('user_model'); // not sure where this is being used...
    $user_id = $CI->session->userdata('user_id');
    if (is_null($user_id)) {
        return false;
    }
    $query = $CI->db->get_where('tbl_users_rights', array('usr_id' => $user_id));
    //$query = $CI->db->query("select * from " . TBL_USERS_RIGHTS . " where usr_id=" . $user_id);
    if ($query->num_rows() == 0) {
         return false;
    }
    $rights = $query->result();
    $user_right = array();
    foreach ($rights as $right) {
        array_push($user_right, $right->sm_id);
    }// print_r($user_right);exit;
    if (in_array($rights_id, $user_right)) {
        return TRUE;
    } else {
        return FALSE;
    }
}