使用未定义变量mysql处理数据库时出错

iyfjxgzm  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(348)

如何处理msql未定义变量错误。这样页面就不会抛出以下内容---
注意:第400行的d:\xampp\htdocs\vbay2\html\vbayshowlisting.php中的未定义变量:cond
注意:第401行的d:\xampp\htdocs\vbay2\html\vbayshowlisting.php中的未定义变量:cond
注意:第402行的d:\xampp\htdocs\vbay2\html\vbayshowlisting.php中的未定义变量:cond
注意:第403行的d:\xampp\htdocs\vbay2\html\vbayshowlisting.php中的未定义变量:cond
我找到了一种方法,在没有准备变量的情况下跳转到我的页面,然后页面抛出这些错误,有没有办法避免页面这样做而转到我的wrror页面。。
我将向您提供完整的pdo代码,但它不是错误的,我只是找到了一种方法,跳转到页面没有一个或两个定义的变量。。

$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();         
                    }
moiiocjp

moiiocjp1#

你可以用

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

q1qsirdb2#

你可以打开 error_reporting(0); 或者仅当值不为空时才指定变量。例如。:

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

相关问题