2017-03-06 2 views
0

의 기능 빈 MySQL의 쿼리를 표시하지 않았나요 :내가있어 PHP

function getNgoIn_the_field_Jobs($ngo_id){ 
global $db; 
return $db->query('SELECT `in_the_field_jobs`.*, `ngo`.`title` AS `ngo_title`, `ngo`.`money` AS `ngo_money`, `ngo`.`time` AS `ngo_time`, `ngo`.`love` AS `ngo_love`, `ngo`.`rating` AS `ngo_rating`, `ngo`.`logo` AS `ngo_logo` 
        FROM `in_the_field_jobs` 
        INNER JOIN `ngo` 
        ON (`ngo`.`ngo_id` = `in_the_field_jobs`.`ngo_id`) 
        WHERE `in_the_field_jobs`.`ngo_id` = ' . (int)$ngo_id); 
} 

을 그리고 내 쿼리는 다음과 같습니다

<?php $in_the_field_jobs = getNgoIn_the_field_Jobs($ngo_id); 

if(!empty($in_the_field_jobs)) { ?> 
    <h5>Work in the Field</h5> 
<?php } ?> 

그래서 나는 현장에서 그 일이 만하는 경우 표시합니다 데이터를 반환하지만, 그것은 0

+2

$ db 란 무엇입니까? 'query' 메쏘드 호출의 결과가 아닌, 함수로부터 질의 결과를 반환하기를 원할 것입니다. 이것은 일종의 DB 연결 객체를 리턴 할 것입니다. 이것이 PDO 객체 인 경우, 쿼리 자체에 이어'fetch()'또는'fetchAll()'을 사용할 필요가 있습니다. – WillardSolutions

+1

(')의 사용법이 약간 혼란 스럽습니다. – Strawberry

+0

코드에 mysql_fetch_array 또는 mysql_fetch_assoc이 포함되어 있습니까? – sukalogika

답변

0

FWIW을 반환하는 경우에도 보여줍니다 ... 작동하지 않습니다, 나는 발견이 쉽게 읽을 수 있습니다 -하지만 대한 바인딩 쿼리를 참조

" 
SELECT j.* 
    , ngo.title ngo_title 
    , ngo.money ngo_money 
    , ngo.time ngo_time 
    , ngo.love ngo_love 
    , ngo.rating ngo_rating 
    , ngo.logo ngo_logo 
    FROM in_the_field_jobs j 
    JOIN ngo 
    ON ngo.ngo_id = j.ngo_id 
WHERE j.ngo_id = $ngo_id; 
" 
0
$con = mysqli_connect("localhost", "user", "password", "database"); 

$result = mysqli_query($con, "SELECT * FROM table_name"); 
$num_rows = mysqli_num_rows($result); 

if ($num_rows == 0) { 
0

사용중인 문자열은 JSON처럼 보이지만 유효하지 않은 것으로 확인되지는 않습니다. 이 모든 문자열이있는 형식 인 경우 ... 그것은 유효한 (한 줄) 할 그것을 편집 ...

{"room": [{"single_room": 1, "twin_room": 3}], "total_amount": [{"amount": 20899}], "travelernum": [{"total": 1 }]} 

OR (멀티 라인) ...

{ 
    "room": [{ 
     "single_room": 1, 
     "twin_room": 3 
    }], 
    "total_amount": [{ 
     "amount": 20899 
    }], 
    "travelernum": [{ 
     "total": 1 
    }] 
} 

JSONLint.com보기 오히려 간단,이 같은 것을 사용하기 때문에 당신의 문자열이 PHP 배열로 변환, 유효한 JSON 형식으로되면, 당신은 ...

... 사용하여 처음으로 유효한 JSON으로

$original_string = '{"room":[{"single_room":1,"twin_room":3}]}{"total_amount":[{"amount":20899}]}{"travelernum":[{"total":1}]}'; 
$new_string = str_replace("}{", ",", $original_string); // replace }{ with , 

그들을 조작 할 수 있습니다

$BookingDetail = json_decode($new_string, true); //true param gets assoc array 

위의 코드를 모두 테스트했는데 작동합니다. JSON 문자열을 PHP 배열로 변환하는 방법에 대한 자세한 내용은 PHP json_decode() 함수 참조를 참조하십시오.
http://php.net/manual/en/function.json-decode.php

0

이것은 호출 된 쿼리 기능에 따라 다릅니다. 이 코드를 보면 말할 수 없습니다. 사용자 정의 DB 클래스 또는 다른 라이브러리가 될 수 있습니다. 그것이 PDO가 아닌 다른 것이라면 여기에 어떤 lib가 사용되는지 아는 것이 중요합니다.

PDO 쿼리 인 경우 문이 작동하지 않으면 FALSE 만 반환됩니다. -> on mysql Error. 비어있는 경우 PDO-Statement 객체를 반환합니다.이 객체는 "비어 있지 않습니다". 단순히 데이터가 없습니다.

http://php.net/manual/de/pdo.query.php

:

PDO :: 쿼리()는 PDOStatement 객체, 실패 할 경우 FALSE를 반환합니다.

// built statement 
$stmt = $db->query('SELECT `in_the_field_jobs`.*, `ngo`.`title` AS `ngo_title`, `ngo`.`money` AS `ngo_money`, `ngo`.`time` AS `ngo_time`, `ngo`.`love` AS `ngo_love`, `ngo`.`rating` AS `ngo_rating`, `ngo`.`logo` AS `ngo_logo` 
       FROM `in_the_field_jobs` 
       INNER JOIN `ngo` 
       ON (`ngo`.`ngo_id` = `in_the_field_jobs`.`ngo_id`) 
       WHERE `in_the_field_jobs`.`ngo_id` = ' . (int)$ngo_id); 
// return the array 
return $stmt->fetchAll(PDO::FETCH_ASSOC); 

트릭을 할해야합니다.