Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用未定义的变量mysql处理数据库时出错_Php_Mysql_Variables - Fatal编程技术网

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

Php 使用未定义的变量mysql处理数据库时出错,php,mysql,variables,Php,Mysql,Variables,如何处理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:\x

如何处理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();         
                    }

您可以打开
错误报告(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.
}

非常感谢我将更新我的代码调查这个错误捕获东西。。错误:语法错误,意外的“e”(T|字符串),应为“|”或变量(T|变量)$count=$stat->rowCount();如果($count==0){header(“location:../imageupload/error.php”);$_SESSION['message']=“Item不再存在”;die();exit()}我发现我没有将$e作为一个变量放入Exception中,所以它应该像这个catch(Exception$e)