我将如何处理Msql未定义变量错误。这样页面就不会抛出以下---
注意:未定义的变量:在第400行的D:\ xampp \ htdocs \ Vbay2 \ html \ vbayshowlisting.php中压缩
注意:未定义的变量:在第401行的D:\ xampp \ htdocs \ Vbay2 \ html \ vbayshowlisting.php中压缩
注意:未定义的变量:在第402行的D:\ xampp \ htdocs \ Vbay2 \ html \ vbayshowlisting.php中进行压缩
注意:未定义的变量:在第403行的D:\ xampp \ htdocs \ Vbay2 \ html \ vbayshowlisting.php中压缩
我找到了一种无需准备变量即可跳入我的页面的方法,然后该页面引发了这些错误,有没有一种方法可以避免该页面执行此操作并转到我的wrror页面。
i会为您提供完整的PDO代码,但不会出错,iv只是找到了一种无需定义变量或两个变量即可跳转到页面的方法。
$db = new PDO("mysql:host=localhost;dbname=nonofyourbusiness", 'root', ''); // 1. set database with this instead of conect - or change conect to this
$query="SELECT * FROM listings WHERE listID=?";
$stat=$db->prepare($query);
if (!$stat){
$_SESSION['message'] = 'Database Request Error vbayshowlisting';
header("location: ../imageupload/error.php");
}
if ($query){
if ($stat->execute(array("$listID"))) {
while($row = $stat->fetch()){
$id=$row['id'];
$sellingtitle=$row['title'];
$sellinginfo=$row['info'];
$sellername=$row['sellername'];
$phone1=$row['phone'];
$town=$row['town'];
$city=$row['city'];
$postcode=$row['postcode'];
$itemaccountname=$row['AccountName'];
if(strlen($postcode) > 8) $postcode = substr($postcode, 0, 8);
if(strlen($town) > 16) $town = substr($town, 0, 16);
if(strlen($city) > 16) $city = substr($city, 0, 16);
$price=$row['price'];
$cond=$row['cond'];
$locate=$row['location'];
if(strlen($locate) > 16) $locate = substr($locate, 0, 16);
$catagory=$row['catagory'];
$date=$row['date'];
$dateadded=$row['dateadded'];
$delivery=$row['delivery'];
$email=$row['email'];
$paypal=$row['paypal'];
if (!empty($paypal)) {$paypal=true;} else {$paypal=false;}
$facebook=$row['facebook'];
if (!empty($facebook)) {$facebook=true;} else {$facebook=false;}
$twitter=$row['twitter'];
if (!empty($twitter)) {$twitter=true;} else {$twitter=false;}
//been set through update sql query vbaysellshowdata
$feedback=$row['feedbackscore'];
$youtubeurl=$row['youtubevideo'];
$i0url=$row['image'];
$i1url=$row['image2'];
$i2url=$row['image3'];
$i3url=$row['image4'];
$i4url=$row['image5'];
//if was true / checked previously / will have url in database = !empty
};
//grabs url of facebook for listing data change to grab from profile in vbaysellmain.php
}
else
{
header("location: ../imageupload/error.php");
echo "ITEM NO LONGER EXISTS";
die();
exit();
}
}
else
{
header("location: ../imageupload/error.php");
echo "ITEM NO LONGER EXISTS";
die();
exit();
}
}
else
{
header("location: ../imageupload/error.php");
echo "No Post Data";
die();
exit();
}
无法捕获错误。...
if (!$stat){
$_SESSION['message'] = 'Database Request Error vbayshowlisting';
header("location: ../imageupload/error.php");
}
我对错误处理的最佳尝试是
//error handler -- edit all mysql querys with this where applicable
$count = $stat->rowCount();
if ($count===0){
header("location: ../imageupload/error.php");
$_SESSION['message']="Item No Longer Exists";
die();
exit();
}
答案 0 :(得分:1)
您可以打开error_reporting(0);
或仅在值不为空时分配变量。例如:
$dateadded= (!empty($row["dateadded"])) ? $row['dateadded'] : "empty here" ;
答案 1 :(得分:0)
您可以使用
try {
$db->prepare($query);
} catch(Exception $e) {
echo $e->getMessage();
}
或者有一个if语句,它会像这样在try部分内部抛出异常
$stat = $db->prepare($query);
try {
if (!$stat) {
throw new Exception('Error message');
} catch(Exception $e) {
echo $e->getMessage();
}
编辑:如果要检查是否设置了变量,则可以使用此条件。
if (isset($cond)) {
// $cond variable is set.
} else {
// $cond variable not set, set it here.
}