当我尝试匹配对象时,映射PDO列不起作用

时间:2017-09-19 20:11:45

标签: php pdo

我正在尝试构建一个API来从mySQL返回数据。这是我第一次使用PDO。

如果我执行以下操作,则只会映射ID而不会映射任何其他ID。我第一次使用这个,所以我不知道在哪里看。此外,我从来没有能够让它在本地运行,所以它在Live网站上运行,所以我想不出一种方法来调试它。

查询结果如下所示

{
ID: "18",
Name: null,
Country: null
}
class cemetery{

    // database connection and table name
    private $conn;

    // object properties
    public $id;
    public $fldcemetery;
    public $fldcountry;      

    public function __construct($db){
        $this->conn = $db;
    }

    public function readOne(){
        // query to read single record
                $query = "select tblcemetery.id, tblcemetery.tblcountry_ID, tblcemetery.fldcemetery, tblcountry.fldcountry   from tblcemetery
                left join tblcountry on
                tblcemetery.tblcountry_ID = tblcountry.ID   
                WHERE
                id = ?
                ORDER BY
                        fldcemetery
                LIMIT 0,1";

        // prepare query statement
        $stmt = $this->conn->prepare( $query );

        // bind id of product to be updated
        $stmt->bindParam(1, $this->id);

        // execute query
        $stmt->execute();

        // get retrieved row
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        // set values to object properties
        $this->fldcemetery = $row['fldcemetery'];
        $this->fldcountry = $row['fldcountry'];     

    }

}
// get database connection
$database = new Database();
$db = $database->getConnection();

// prepare cemetery object
$cemetery = new cemetery($db);

// set ID property of record to read
$cemetery->id = isset($_GET['id']) ? $_GET['id'] : die();

// read the details of cemetery to be edited
$cemetery->readOne();

// create array
$cemetery_arr = array(
    "ID" =>  $cemetery->id,
    "Name" => $cemetery->fldcemetery,
    "Country" => $cemetery->fldcountry
);

// make it json format
print_r(json_encode($cemetery_arr));

2 个答案:

答案 0 :(得分:0)

我认为这些值的关键必须是$row['tblcemetery.fldcemetery']

在sql查询中使用as来更改结果集中的键。

答案 1 :(得分:0)

<a href="./getVendors?category=CATEGORY_VALUE">CATEGORY VALUE</a>

应该是

WHERE id = ?