2016-11-09 1 views
1

私はMySQLからデータを取得するクラスの関数を持っています。すべて正常に動作しますが、MySQLデータの列名を取得する方法も知りたいと思っています。ここでMySQLのfileds名を取得するPHP PDO

は私のコードです:

public static function getTickets(){ 
    $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); 
    $sql = "select tickets.*,customers.* from tickets,customers where 
      (tickets.ticket_customer_id = customers.customer_id) order by tickets.ticket_open_date desc "; 

    $st = $conn->prepare($sql); 
    $st->execute(); 
    $list = array(); 

    while($row=$st->fetch()) { 
     $tickets = New Tickets($row); 
     $list[] = $tickets; 
    } 

    //total rows of customer 
    $sql = "select FOUND_ROWS() as totalRows"; 
    $totalRows = $conn->query($sql)->fetch(); 
    $conn=null; 

    $columnCount = $st->columnCount(); 

    //pass the values to page 
    return (array("results"=>$list ,"totalRows"=>$totalRows,"columnCount"=>$columnCount)); 
} 

答えて

0

それはあなたがここで必要なものを伝えるのは難しいですが、あなたのコードを見て、私はあなたが非常にデータから取得することができますあなたが必要とするすべてはあなたが

を取得していると言うでしょう
public static function getTickets($conn){ 
    $sql = "select tickets.*,customers.* from tickets,customers where 
    (tickets.ticket_customer_id = customers.customer_id) order by tickets.ticket_open_date desc "; 
    $st = $conn->query($sql); 

    $list = array(); 
    while($row=$st->fetch(PDO::FETCH_ASSOC)) { 
     $list[] = New Tickets($row); 
     $columnNames = array_keys($row); 
    } 

    //total rows of customer 
    $totalRows = count($list); 
    $columnCount = count($columnNames); 

    //pass the values to page 
    return (array("results"=>$list ,"totalRows"=>$totalRows,"columnCount"=>$columnCount)); 
} 
関連する問題