2012-03-27 3 views
1

WHERE 문에 두 개의 변수가 있습니다. 구문 오류가 발생하여 공간을 구분할 수 없습니다. 도와 주셔서 감사합니다.내 SQL 문에 공백을 가져올 수 없습니다.

이 BTW 나는 $ 공간 변수를 설정 시도했다 (내가 CodeIgniter를 사용하고 있습니다), 그리고 전에 공백을 넣고, 두 변수를 설정 한 후, 그리고 SQL있다.

ERROR

오류 번호 : 당신은 당신의 SQL 구문에 오류가 1064

; 'source_adusers.ad_account = "근처에서 사용할 올바른 구문에 대해서는 MySQL 서버 버전에 해당하는 매뉴얼을 확인하십시오. Wolfs, Marc"GROUP BY rollout_systems.eam_user LIMIT "2 행

SELECT *, COUNT (rollout_systems.EAM_USER) rollout_systems FROM 시스템은 LEFT rollout_systems.EAM_User = source_adusers.ad_account ON source_adusers 가입으로 WHERE rollout_systems.eam_user의 LIMIT BY rollout_systems.scope_ID = 3AND source_adusers.ad_account = "Wolfs이, 마크"GROUP 0,50

줄 번호 : (330)

PHP

if ($this->session->userdata('scopeId') != NULL) { 
     $where1 = 'WHERE rollout_systems.scope_ID = '. $this->session->userdata('scopeId') . ''; 
    } else { 
     redirect('/headquarters/home');; 
    } 

    if ($search) { 
     $where2 = ' AND rollout_systems.sys_name ="'.$search.'"'; 
    } else { 
     $where2 = ''; 
    } 
$query = $this->db->query('SELECT * FROM rollout_systems LEFT JOIN source_adusers 
     ON rollout_systems.eam_user = source_adusers.ad_account '. $where1 .''. $where2 .' GROUP BY rollout_systems.sys_name LIMIT '.$limit.',50'); 

답변

2

공백과 AND를 $ 쿼리에 저장하고 대신 where 변수에 넣으면 어떨까요? 그렇다면 2가 쿼리에 영향을주지 않고 작동해야하는 $이므로 0 = 0입니다.

if ($this->session->userdata('scopeId') != NULL) { 
     $where1 = 'WHERE rollout_systems.scope_ID = '. $this->session->userdata('scopeId') . ''; 
    } else { 
     redirect('/headquarters/home');; 
    } 

    if ($search) { 
     $where2 = 'rollout_systems.sys_name ="'.$search.'"'; 
    } else { 
     $where2 = '0=0'; 
    } 
$query = $this->db->query('SELECT * FROM rollout_systems LEFT JOIN source_adusers 
     ON rollout_systems.eam_user = source_adusers.ad_account '. $where1 .' and '. $where2 .' GROUP BY rollout_systems.sys_name LIMIT '.$limit.',50'); 
1

그냥 두 바르 사이에 공백을 추가 - '. $where1 .' '. $where2 .'

으로는 정수 값을 기대하는 경우 당신이 정말로 mysql_real_escape_string() 또는 intval()를 사용하여 사용자 입력을 탈출해야 다른 사람에 의해 지적했다. PDO 또는 mysqli를 사용하는 경우 준비된 문을 사용하십시오. $this->db가 PDO의 인스턴스 인 경우

당신은 사용할 수 있습니다 -

$params = array(); 

if ($this->session->userdata('scopeId') != NULL) { 
    $where = 'WHERE rollout_systems.scope_ID = ?'; 
    $params[] = $this->session->userdata('scopeId'); 
} else { 
    redirect('/headquarters/home');; 
} 

if ($search) { 
    $where .= ' AND rollout_systems.sys_name = ?'; 
    $params[] = $search; 
} 

$sql = "SELECT * FROM rollout_systems 
    LEFT JOIN source_adusers ON rollout_systems.eam_user = source_adusers.ad_account 
    $where 
    GROUP BY rollout_systems.sys_name 
    LIMIT ?, 50"; 
$params[] = $limit; 

$query = $this->db->prepare($sql); 
$query->execute($params); 
0
$where = array(); 

if ($this->session->userdata('scopeId') != NULL) { 
    // better to escape your parameter here unless you trust it totally 
    // judging by its name, it's user-provided data, so I wouldn't trust it 
    $where[] = 'rollout_systems.scope_ID = '. $this->session->userdata('scopeId'); 
} else { 
    redirect('/headquarters/home'); 
    // you may need to exit here after redirection, depends on your implementation 
} 

if ($search) { 
    // once again don't forget to escape $search in real application 
    $where[] = "rollout_systems.sys_name = '" . $search . "'"; 
} 

$query = $this->db->query(" 
    SELECT 
     * 
    FROM 
     `rollout_systems` 
     LEFT JOIN 
     `source_adusers` 
     ON rollout_systems.eam_user = source_adusers.ad_account 
    WHERE 
     " . implode (' AND ', $where) . " 
    GROUP BY 
     rollout_systems.sys_name 
    LIMIT " . $limit /* escape it! */ . ",50" 
); 
0

또한 PHP 구문을 사용하는 옵션이

$sql = " blah blah {$my_var} {$my_other_var} "; 
관련 문제