2017-12-21 6 views
0

PDO를 사용하고 try 블록에 쿼리를 넣었습니다. 준비하고 다음에 fetchAll(PDO::FETCH_ASSOC)을 호출합니다. 그 줄 안에 내가 변수가 $obj 인 PHP를 foreach 루프에 넣은 텍스트가 필요합니다. 그런 다음 "my name", "price"및 "code"의 참조로 변수 $obj을 내 코드에 넣었을 때 전화를 "생각"했습니다. 단지 쿼리 문자열 object(PDOStatement)#2(1){["queryString"]}=>...the Sql statmement.PHP PDO 쿼리 문제가 object => "queryString"으로 반환됩니다.

try { 
    $tools = $dbh->prepare("SELECT t.item_code as code, t.item_name as 
name, t.retail_price as retail, 
          t.sale_price as price, t.item_pieces as 
pieces, t.qty as quantity, 
          t.sold as sold, b.brand as brand, 
c.category as category 
           FROM Tools AS t 
           JOIN Images AS i ON t.t_id = i.t_id 
            JOIN Brands AS b ON t.b_id = b.b_id 
           JOIN Categories AS c ON t.c_id = 
c.c_id 
           LEFT OUTER JOIN Types as tt ON tt.t_id = t.tt_id"); 
    $tools->execute(); 
    $tools->fetchAll(PDO::FETCH_ASSOC); 
}catch (PDOException $e) { 
    echo 'unable to retrieve data'; 
    echo $e->getMessage(); 
    exit(); 
} 
<?php 
        foreach($tools as $obj) { 
        ?> 
        <div class="col-xs-12 col-sm-6 col-md-3"> 
         <article class="card"> 
          <p class="text-center"> 
           Item: <?php echo $obj->name; ?><br> 
           Brand: <br> 
           Price: <?php echo $obj->price; ?><br> 
           <button class="btn btn-default btn-lg" 
value="<?php echo $obj->name; ?>"> 
            <a href="#"><?php echo $obj->code; 
?></a> 
           </button> 
          </p> 
         </article> 
        </div> 
        <?php } ?> 

웹 페이지를 withing에 코드 메시지를 뱉어의 오류입니다.

`object(PDOStatement)#2 (1) { ["queryString"]=> string(609) "SELECT t.item_code as code, t.item_name as name, t.retail_price as retail, t.sale_price as price, t.item_pieces as pieces, t.qty as quantity, t.sold as sold, b.brand as brand, c.category as category FROM Tools AS t JOIN Images AS i ON t.t_id = i.t_id JOIN Brands AS b ON t.b_id = b.b_id JOIN Categories AS c ON t.c_id = c.c_id LEFT OUTER JOIN Types as tt ON tt.t_id = t.tt_id" }` 

답변

1

$tools->fetchAll(PDO::FETCH_ASSOC);이 문제입니다. fetchAll은 명령문에서 모든 행을 반입하고이를 리턴합니다. 할 수 있습니다 foreach($results... 대신 다른 방법으로 foreach($tools...


, 당신이해야 다음

$results = $tools->fetchAll(PDO::FETCH_ASSOC); 

: 당신이 그것을 사용하려고하는 경우처럼, 다른 변수에 그 표현의 결과를 할당해야 fetchAll 표현식을 삭제할 수 있습니다. PDOStatement는 traversable을 구현하고 fetch 메소드를 명시 적으로 사용하지 않고도 foreach가 실행 된 후 직접 반복 할 수 있습니다.

+0

감사합니다. 감사드립니다. – mikeyjhavoc