So I have table RACUN
and I want to select multiple rows where USERID = something
I have this query
<?php
class Racun{
// database connection and table name
private $conn;
private $table_name = "RACUN";
// object properties
public $racunId;
public $userId;
public $broj;
public $datum;
public $vrednost;
public $vrsta;
// constructor with $db as database connection
public function __construct($db){
$this->conn = $db;
}
function read(){
// select all query
$query = "SELECT
RACUNID, USERID, BROJ, DATUM, VREDNOST, VRSTA
FROM
" . $this->table_name . "
WHERE
USERID = ?";
// prepare query statement
$stmt = $this->conn->prepare($query);
$stmt->bindParam(1, $this->userId);
// execute query
$stmt->execute();
return $stmt;
}
}
It returns me that nothing has been found.
When I change $query
to:
$query = "SELECT
RACUNID, USERID, BROJ, DATUM, VREDNOST, VRSTA
FROM
" . $this->table_name . "";
It returns me:
{"records":[{"racunId":"1","userId":"1","broj":"1200089","datum":"2018-03-09","vrednost":"127000","vrsta":"0"}]}
here is php
file that runs read()
function:
<?php
// required headers
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/Racun.php';
// instantiate database and product object
$database = new Database();
$db = $database->getConnection();
// initialize object
$racun = new Racun($db);
// query users
$stmt = $racun->read();
$num = $stmt->rowCount();
// check if more than 0 record found
if($num>0){
// products array
$racuns_arr=array();
$racuns_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);
$racun_item=array(
"racunId" => $RACUNID,
"userId" => $USERID,
"broj" => $BROJ,
"datum" => $DATUM,
"vrednost" => $VREDNOST,
"vrsta" => $VRSTA
);
array_push($racuns_arr["records"], $racun_item);
}
echo json_encode($racuns_arr);
}
else{
echo json_encode(
array("message" => "Nothing found.")
);
}
?>