错误处理带有未定义变量mysql的数据库



如何处理Msql未定义变量错误。这样页面就不会抛出以下内容--

注意:第400行中D:\examplep\htdocs\Vbay2\html\vbayshowlisting.php中的未定义变量:cond

注意:第401行中D:\examplep\htdocs\Vbay2\html\vbayshowlisting.php中的未定义变量:cond

注意:未定义的变量:第402行的D:\examplep\htdocs\Vbay2\html\vbayshowlisting.php中的cond

注意:第403行中D:\examplep\htdocs\Vbay2\html\vbayshowlisting.php中的未定义变量:cond

我找到了一种方法,可以在没有准备变量的情况下跳转到我的页面,然后页面抛出这些错误,有没有办法避免页面这样做,转而转到我的wrror页面。。

我将向您提供完整的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();         
}

您可以打开error_reporting(0);,也可以仅在值不为空时分配变量。例如:

$dateadded= (!empty($row["dateadded"])) ? $row['dateadded'] : "empty here" ;

您可以使用

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.
}

最新更新