未定义的变量

时间:2014-05-11 17:23:06

标签: php pdo

我不知道如何描述问题。我正在尝试获取并且我在PHPmyAdmin中尝试了查询,它很好地返回结果但是当我将它传输到PHP时。

错误是未定义的变量。

这是我的代码。请帮助我,我被困在这里2个小时谢谢你!

$targetint_id = $_GET["int_id"]; 
$targetbrch_id = $_GET["brch_id"];
$targetcst_id = $_GET["cst_id"]; 
$targetcbrch_id = $_GET["cbrch_id"]; 
$targetdateadded = $_GET["dateadded"]; 
$targettimeadded = $_GET["timeadded"]; 

$stmt = $db->prepare("SELECT * FROM tbl_soldinventory where int_id= :int and brch_id= :brch and cst_id= :cst and cbrch_id= :cbrch and date= :date and timeadded= :time");
$stmt->execute(array(':int' => "$targetint_id", ':brch' => "$targetbrch_id",':cst' => "$targetcst_id", ':cbrch' => "$targetcbrch_id",':date' => "$targetdateadded", ':time' => "$targettimeadded"));

while($rows = $stmt->fetch(PDO::FETCH_ASSOC))
{
    $dateadded = $rows["date"];
    $timeadded = $rows["timeadded"];
    $quantity = $rows["quantity"];
}
echo $dateadded;

1 个答案:

答案 0 :(得分:0)

执行查询时,您的代码错过了错误处理。我在这里添加了一些示例,我假设您报告警告和NULL值,查询只是不返回任何行。

也许是因为它失败了,所以你需要检查并处理这种情况,例如:通过收集错误信息并显示查询以实际调试更容易(不要让自己失明):

$result = $stmt->execute(array(
    ':int'  => "$targetint_id",    ':brch'  => "$targetbrch_id", 
    ':cst'  => "$targetcst_id",    ':cbrch' => "$targetcbrch_id", 
    ':date' => "$targetdateadded", ':time'  => "$targettimeadded",
));

if (FALSE === $result) {
    var_dump($stmt->queryString);
    throw new RuntimeException(vsprintf(
        '[SQLSTATE %s] %d %s', $stmt->errorInfo()
    ));
}

此外,请参阅此参考问题:

具有该更改的完整代码:

$targetint_id    = $_GET["int_id"];
$targetbrch_id   = $_GET["brch_id"];
$targetcst_id    = $_GET["cst_id"];
$targetcbrch_id  = $_GET["cbrch_id"];
$targetdateadded = $_GET["dateadded"];
$targettimeadded = $_GET["timeadded"];

$stmt = $db->prepare(
    "SELECT *
     FROM tbl_soldinventory
     WHERE
       int_id= :int and brch_id= :brch and cst_id= :cst
       and cbrch_id= :cbrch and date= :date and timeadded= :time
    "
);
$result = $stmt->execute(array(
    ':int'  => "$targetint_id",    ':brch'  => "$targetbrch_id", 
    ':cst'  => "$targetcst_id",    ':cbrch' => "$targetcbrch_id", 
    ':date' => "$targetdateadded", ':time'  => "$targettimeadded",
));

if (FALSE === $result) {
    var_dump($stmt->queryString);
    throw new RuntimeException(vsprintf(
        '[SQLSTATE %s] %d %s', $stmt->errorInfo()
    ));
}

$dateadded = $timeadded = $quantity = NULL;

foreach ($stmt as $row)
{
    $dateadded = $row["date"];
    $timeadded = $row["timeadded"];
    $quantity  = $row["quantity"];
}

var_dump($dateadded, $timeadded, $quantity);