【问题标题】:PDO returning nothingPDO 什么都不返回
【发布时间】:2015-06-26 19:35:06
【问题描述】:

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

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

注意:尝试在第 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

【问题讨论】:

  • $results['series'] 中有什么内容?
  • 不应该 $results['series']-&gt;description$series-&gt;description 吗?
  • 您可能还想在foreach 循环中尝试var_dump($series) 看看它是什么样子的。
  • $resultsindex.php 函数中的局部变量。您无法在homepage.php 中访问它。另外,我看不到homepage.php 包含index.php 的位置。
  • 我快速查看了您的构造函数的代码,看起来您使用 _construct 代替了 __construct 。除了@Barmar 的答案所建议的更改之外,您是否想更改它并查看它是否有任何不同? php.net/manual/en/language.oop5.decon.php

标签: php mysql pdo


【解决方案1】:

这个循环是错误的:

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

最后一行应该是:

    $list[] = $series;

以便它将每个系列推到数组上。您的代码每次通过循环都将数组替换为一个系列。

【讨论】:

  • 感谢这个答案帮助解决了我的部分问题,但是当我尝试 var dump 它仍然给我空值。 pastebin.com/iwW5k3j9
  • 如果一切顺利,是否应该显示没有结果?因为我什么也得不到。
  • 如果一切顺利,那么您肯定会看到一些结果。除了 PDO 的异常处理之外,您是否为代码打开了 PHP 错误报告?
  • @JonattanDavelaar 请参阅上面关于__construct() 中的错字的评论。
猜你喜欢
  • 2012-06-18
  • 2018-09-10
  • 2017-02-14
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多