试图回显2个变量,但是两个值都得到了“意外的索引”。 我认为回声有问题。我在错误地要求$ faktura对象中的值。但是我不知道我在做什么错。
这是我的错误消息:
Notice: Undefined index: userID in C:\Faktura\scripts\faktura.php on line 21
Notice: Undefined index: fakturaNr in C:\Faktura\scripts\faktura.php on line 22
faktura.php
<?php
session_start();
require("config/database.php");
if(isset($_POST['fakturaBtn'])){
$fakturaNr = 50163;
$userID = 144;
$stmt = $pdo->prepare('SELECT * FROM myView WHERE fakturaNr = :fakturaNr AND userId = :userID');
$stmt->execute(['fakturaNr' => $fakturaNr, 'userID' => $userID]); //Where the exception occurs
$faktura = $stmt->fetch();
echo $faktura['userID'];
echo $faktura['fakturaNr'];
}
?>
database.php
<?php
$host = 'myhost';
$db = 'mydb';
$user = 'myuser';
$pass = 'mypass';
$dsn = "sqlsrv:Server=$host;Database=$db";
//$dsn = "sqlserv:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_UTF8,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
?>
我尝试使用带有isset的if语句,但这也不起作用。 这是我尝试的方法
if(isset($faktura)) {
echo $faktura['userID'];
echo $faktura['fakturaNr'];
}