-2

我有以下脚本根本不返回任何东西,我得到的最好的是一个 var_dump 说 bool(false).. 我的 error_log 中什么都没有,也没有抛出异常:

索引.php:

require("php/bootstrap.php");
$campaign = new Campaign($_GET['campaign'], $mysql);

引导程序.php:

define ("MYSQL_USER", "specialagent");
define ("MYSQL_PASS", "supertopsecret");
define ("MYSQL_HOST", "127.0.0.1");
define ("MYSQL_PORT", "3306");
define ("MYSQL_DB", "databasewithsecretinformation");


/* Auto include classes on instantiation */
function __autoload($class_name) {
    include 'classes/'.$class_name.'.php';
}

/* Connections */
try {
$mysql = new PDO("mysql:host=".MYSQL_HOST.";dbname=test", MYSQL_USER, MYSQL_PASS);
} catch(PDOException $e) {
die("There was a problem connecting to the database. Error: " . $e->getMessage());
}

Campaign.php

class Campaign {
protected $dbh;
private $campaign_id = "";
private $name = "";

public function __construct ($campaign_id, PDO $db) {

    //if ($campaign_id) {
        $this->setCampaignID($campaign_id);
        $this->dbh = $db;
        $this->loadCampaignInfo();
    //}
}


public function getCampaignID() {
    return $this->campaign_id;
}

public function setCampaignID($id) {
    $this->campaign_id = $id;
}

public function __destruct() {
    //$this->dbh = null;
}

public function loadCampaignInfo() {
    /* Get info from database */
    $sql = "SELECT * FROM campaigns WHERE campaign=:campaignID";

    try {
        echo "trying query...$sql<br />";
        if ($this->dbh instanceof PDO) {
            $stmt = $this->dbh->prepare($sql);
            $stmt->bindParam(":campaignID", $this->campaign_id, PDO::PARAM_STR);
            $stmt->execute();
            $row = $stmt->fetch();

            echo "Returned Data: ";
            var_dump($row);
        } else {
            echo "Not a valid PDO resource";
        }


    } catch (PDOException $e) {
        echo "Problem : " . $e->getMessage;
    } catch (Exception $e) {
        echo "Other problem : " . $e->getMessage;
    }


    /* Set properties */
}

}

输出:

trying query...SELECT * FROM campaigns WHERE campaign=:campaignID
Returned Data: bool(false) 
4

2 回答 2

0

该问题与 PDO 无关。
Returned Data: bool(false)表示没有找到符合条件的行。
检查您的表是否包含数据并检查输入。
检查您是否从自动加载和所有类似的东西中获得了正确的类。

此外,将您的引导连接更改为

ini_set('display_errors',1);
error_reporting(E_ALL);

/* Connections */
$mysql = new PDO("mysql:host=".MYSQL_HOST.";dbname=test", MYSQL_USER, MYSQL_PASS);
$mysql->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

您可能还希望使该方法也不那么臃肿:

/* Get info from database */
public function loadCampaignInfo()
{
    $sql = "SELECT * FROM campaigns WHERE campaign=?";
    $stmt = $this->dbh->prepare($sql);
    $stmt->execute([$this->campaign_id]);
    return $stmt->fetch();
}
于 2013-11-08T18:28:54.110 回答
-1

问题是用户没有正确的权限来访问数据库,在你的常识表明凭据是问题之后,登录到 MySQL cli 并注意到数据库不在 SHOW DATABASES 中。

谢谢大家的帮助。

于 2013-11-08T18:59:54.623 回答