PDO什么也没有回来

时间:2015-06-26 19:35:06

标签: php mysql pdo

所以基本上我已经开始使用OOP了,我根据教程编写的脚本给了我两个问题:

当我尝试将对象转换为变量时,我收到此错误:

  

注意:尝试在第14行的/home/stream/V2/templates/homepage.php中获取非对象的属性

第14行的代码是:

<a href=".action=viewSeries&amp;seriesId=<?php echo $series->id ?>"><?php echo htmlspecialchars( $series->id ) ?></a>

其次,不是将对象变成变量而是试图直接从中访问数据,而我却什么也没得到。

我基本上有3个页面来帮助从表中获取数据:

Homepage.php

    <?php include( "templates/include/header.php" ); ?>

    <ul id="headlines">

    <?php
    echo $results['totalRows'];
    foreach( $results['series'] as $series ){ ?>

    <li>
    <h2>
    <!-- remember to add htmlspecialchars -->
    <span class="title"><?php echo $results['series']->description ?></span> 

    <a href=".action=viewSeries&amp;seriesId=<?php echo $series->id ?>"><?php echo htmlspecialchars( $series->id ) ?></a>
    </h2>
    </li>


    <?php 
    } ?>

    </ul>

    <p><a href="./?action=archive">Series Archive</a></p>
    <?php include( "templates/include/footer.php" ); ?>

的index.php

    <?php

require( "core/config.php" );
$action = isset ( $_GET['action'] ) ? $_GET['action'] : "";

switch ( $action ){
    case 'series':
        series();
    break;

    case 'viewSeries':
        viewSeries();
    break;

    default:
        homepage();
}

function series(){
    $results = array();
    $data = Series::getList();
    $results['series'] = $data['results'];
    $results['totalRows'] = $data['totalRows'];
    $results['pageTitle'] = "Series Title";
    require( TEMPLATE_PATH . "/series.php" );
}   

function viewSeries(){
    if( !isset( $_GET['seriesId'] ) || !$_GET['seriesId'] ){
        homepage();
        return;
    }

    $results = array();
    $results['series'] = Series::getBydId( (int)$_GET['seriesId'] );
    $results['pageTitle'] = $results['series']->title . "| Sub title";
    require( TEMPLATE_PATH . "/viewSeries.php" );
}

function homepage(){
    $results = array();
    $data = Series::getList( HOMEPAGE_NUM_SERIES );
    $results['series'] = $data['results'];
    $results['totalRows'] = $data['totalRows'];
    $results['pageTitle'] = "Streamaton Homepage";
    require( TEMPLATE_PATH . "/homepage.php");
}

这是Series.php类的代码:

    //Return all (or range of) Series objects in the db
public static function getList( $numRows=100000, $order="id DESC" ){
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT id, description FROM series ORDER BY :order LIMIT :numRows";
    //SQL_CALC_FOUND_ROWS *
    $st = $conn->prepare( $sql );
    $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
    $st->bindValue( ":order", $order, PDO::PARAM_STR );
    $st->execute();
    $list = array();

    while( $row = $st->fetch() ){
        $series = new Series( $row );
        $list = $series;    
    }


    //Now get the total number of series that match the criteria
    $sql = "SELECT FOUND_ROWS() AS totalRows";
    $totalRows = $conn->query( $sql )->fetch();
    $conn =  null;
    return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );

}

整个series.php代码如下所示:http://pastebin.com/f95A8NPr

1 个答案:

答案 0 :(得分:2)

这个循环错了:

while( $row = $st->fetch() ){
    $series = new Series( $row );
    $list = $series;    
}

最后一行应该是:

    $list[] = $series;

以便将每个系列推送到阵列上。每次循环时,你的代码都会用一个系列替换数组。