Код: Выделить всё
class Food {
// Properties
public $id;
public $version;
public $name;
public $solid;
public $servingtext;
public $servingmeasure;
public $servingamount;
private function __construct__() {}
public static function get_with_id ($dbconn,$id, $version) {
$stmt = $dbconn->prepare('SELECT * FROM Foods WHERE Id = :Id and Version = :Version;');
$stmt->execute([':Id' => $id, ':Version' => $version]);
return $stmt->fetchObject(__CLASS__);
// return $answer;
}
// Methods
function set_id($id){
$this->id = $id;
}
etc. with sets and gets
Код: Выделить всё
require "./templates/header.php";
try
{
include __DIR__ . '../../connection.php';
$conobj = new Connection();
$dbconn = $conobj->get_connection();
} catch (Throwable $t) {
error_log(__FILE__ . ':' . $t->getMessage());
echo 'Connection Error ' . $t->getMessage();
}
require "./models/food.php";
$food = Food::get_with_id ($dbconn,2,0);
var_dump($food);
Код: Выделить всё
object(Food)#4 (14) { ["id"]=> NULL ["version"]=> NULL ["name"]=> NULL ["solid"]=> NULL ["servingtext"]=> NULL ["servingmeasure"]=> NULL ["servingamount"]=> NULL ["Id"]=> int(2) ["Version"]=> int(0) ["Name"]=> string(35) "Sanitarium Weet-Bix Blends Hi-Bran+" ["Solid"]=> int(1) ["ServingText"]=> string(28) "1 serving = 40g (2 biscuits)" ["ServingMeasure"]=> string(1) "g" ["ServingAmount"]=> int(40) }Я пробовал..
Код: Выделить всё
Подробнее здесь: [url]https://stackoverflow.com/questions/79276892/php-pdo-fetchobject-returns-duplicated-properties-half-of-them-null[/url]
Мобильная версия