-3

我正在做一个小项目,我需要从每一行返回一组值,但只从表中的一列(页面)返回。

我的方法如下所示:

public static function getMainArray() {
  $conn = parent::connect();
  $sql = "SELECT page FROM " . TBL_PAGES . " WHERE level = 0";

  try {
    $st = $conn->prepare( $sql );
    $st->execute();
    $pages = array();
    foreach ( $st->fetchAll() as $row ) {
      $pages[] = new Page( $row );
    }     
    parent::disconnect( $conn );
    return array( $pages);
  } catch ( PDOException $e ) {
    parent::disconnect( $conn );
    die( "Query failed: " . $e->getMessage() );
  }
}

当我打印这个数组时,

$pages = array();
$pages = Page::getMainArray();
print_r($pages);

输出如下:

Array ( 
[0] => Array ( 
  [0] => Page Object ( [data:protected] => Array ( 
    [id] => 
    [page] => home
    [url] => 
    [level] => 
    [parent] => 
    [nazov] => 
    [title_navi] => 
    [title_meta] => 
  ) 
)
  [1] => Page Object ( [data:protected] => Array ( 
    [id] => 
    [page] => about
    [url] => 
    [level] => 
    [parent] => 
    .
    .
    .

我需要改变什么,所以输出将是这样的:

Array ( 
  [0] => home
  [1] => about
  [2] => orders
  [3] => info
  [4] => contact
)
4

2 回答 2

0

它必须是什么,没有所有无用的代码

public static function getMainArray() {
  $conn = parent::getConnection();
  $sql = "SELECT page FROM " . TBL_PAGES . " WHERE level = 0";
  $st = $conn->prepare( $sql );
  $st->execute();
  return $st->fetchAll(PDO::FETCH_COLUMN, 0);
}

请注意,parent::getConnection()方法不应在每次调用时都连接,而应始终返回已打开的连接。parent::disconnect()根本不应该在这里使用。以及尝试..catch。

于 2013-07-02T11:03:04.067 回答
-1

改变

$pages[] = new Page( $row );

$pages[] = $row['page'];
于 2013-07-02T10:51:52.343 回答