2016-11-09 52 views
1

我有一个从MySQL获取数据的类的函数。所有的作品都可以,但我也想知道如何获取MySQL数据的列名。PHP PDO获取MySQL文件名

这里是我的代码:

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)); 
}