0

I am attempting to build an API to return data from mySQL. This is one of my first times using PDO.

I am attempting to pass an ID to this below function in order to return all names from tblnames where tblcemetery_ID = whatever is passed to the page.

I have confirmed that the SQL itself is sound..

Any pointers would be appreciated.

The results always says

{"message":"No Casualties found."}

<?php
class casualty{

    // database connection and table name
    private $conn;
    private $table_name = "tblcasualty";

    // object properties
    public $id;
    public $fldgraphicname;


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

    // used by select drop-down list
    public function cemetery(){            
        // query to read single record
                    $query = "SELECT *
                        FROM 
                        `mapleleaf`.`tblnames` 
                            WHERE
                        `tblcemetery_ID` = ?
                            ORDER BY ID
                         ";

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

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

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

            return $stmt;
    }         

}
?>
<?php
// required header
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

// include database and object files
include_once '../config/database.php';
include_once '../objects/casualty.php';

// instantiate database and casualty object
$database = new Database();
$db = $database->getConnection();

// initialize object
$casualty = new casualty($db);

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

// query categorys
$stmt = $casualty->cemetery();
$num = $stmt->rowCount();

// check if more than 0 record found
if($num>0){

    // products array
    $casualtys_arr=array();
    $casualtys_arr["records"]=array();

    // retrieve our table contents
    // fetch() is faster than fetchAll()
    // http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        // extract row
        // this will make $row['name'] to
        // just $name only
        extract($row);

        $casualty_item=array(
            "ID" => $ID,
            "filename" => $fldgraphicname                    
        );

        array_push($casualtys_arr["records"], $casualty_item);
    }

    echo json_encode($casualtys_arr);
}

else{
    echo json_encode(
        array("message" => "No Casualties found.")
    );
}
?>
Tim Cadieux
  • 447
  • 9
  • 21

1 Answers1

1

You bindParam function is missing a parameter, since you are not using named binds. The correct would be $stmt->bindParam(1, $this->id, PDO::PARAM_INT);.

Link to documentation: http://php.net/manual/en/pdostatement.bindparam.php

faabiopontes
  • 106
  • 6