drzbc6003 2015-08-02 08:53
浏览 60
已采纳

我是PHP的新手,我不明白为什么我得到这个未定义的变量错误

This is the relevant part of my code and the variable issue arises with results_string. I will strong/emphasis the exact line but I wanted to give the full scope of it as well.

Edit: Apparently I can't strong/emphasis code so the three *** will have to do to signify where the error happens.

It is really annoying me that I can't figure out this simple issue. I really appreciate any help that is given here.

$database = Database_Factory::create(1);
$database->getConnection($_SESSION['url'],$_SESSION['username'],$_SESSION['password'],$_SESSION['database']);
$results_string;

if(isset($_SESSION['query'])){
    $_SESSION['results'] = $database->run_query($_SESSION['query']);
    if(!is_string($_SESSION['results'])){
        $results = $_SESSION['results'];
        $field_count = $results->field_count;
        $fields = $results->fetch_fields();

         $results_string = '<table border=1><tr style="color:red; background-color: #b0c4de">';
        foreach($fields as $field){
            $results_string .= '<td>'.$field->name.'</td>';
        }
        $results_string .= '</tr>';
        while($row = $results->fetch_row()){
            $results_string .= '<tr style="color:orange; background-color: #e0ffff">';
            for($i = 0; $i < $field_count; $i++){
                $results_string .= '<td>'.$row[$i]."</td>";
            }
            $results_string .= '</tr>';
        }
        $results_string .= '</table>';
    }else{
       $results_string = "<h2 style='color:red;'>".$_SESSION['results']."</h2>";
    }
    unset($_SESSION['query']);
}else if(isset($_SESSION["update"])){
    if(preg_match('/^insert into shipments/',strtolower($_SESSION['update'])) || preg_match('/^update shipments/',strtolower($_SESSION['update']))){ 
        $supplier_snum;
        $updateSuppliers = false;    

       $first = strpos($_SESSION['update'],"(")+1;
        $last = strpos($_SESSION['update'],")");


        $temp = substr($_SESSION['update'],$first,$last);
        $temp = preg_replace('/\)/', '',$temp);
        $temp = preg_replace('/\;/','',$temp);
        $temp = preg_replace('/\'/','',$temp);
        $temp = preg_replace("/\s+/","",$temp);
        $values = explode(",",$temp);
        foreach($values as $val){
            if($val >= 100){
                $updateSuppliers = true;
            }else if(preg_match('/^S/',$val)){
                $supplier_snum = $val;
            }
        }
    }


    $_SESSION['results'] = $database->run_update($_SESSION['update']);
    if(is_numeric($_SESSION['results'])){
        $results_string .= '<h2 style="color:green">'.$_SESSION['results'].' number of rows have been successfully updated!</h2>';
        if($updateSuppliers){
            $snums = $database->run_query("select DISTINCT(suppliers.snum) from suppliers join shipments on suppliers.snum = shipments.snum and shipments.quantity >= 100");
            $csl_snums .= "'".$supplier_snum."'";
            while($row = $snums->fetch_row()){
                for($i = 0; $i < $snums->field_count; $i++){
                    $csl_snums .= ",'".$row[0]."'";
                }
            }
            $blah = $database->run_update("UPDATE suppliers set status = (status+ 5) where snum IN (".$csl_snums.")");
            $results_string .='<br /><h2 style="color:green"> Business Logic Dectected! '.$blah.'</h2>';
        }
    }else{
        ***$results_string .= '<h2 style="color:red">'.$_SESSION['results'].'</h2>';***
    }
    unset($_SESSION['update']);
}else if(isset($_POST['logout'])){
        session_destroy();
        header('Location:index.php');
}

The error is

[Sun Aug 02 04:29:47.726659 2015] [:error] [pid 8108:tid 748] [client ::1:63388] PHP Notice: Undefined variable: results_string in C:\Apache24\htdocs\Prototype\New Working Prototype\QueryResults.php on line 119

The error happens on this line (As I said at the start, I went ahead and put strong/emphasis to make it clear in the code above):

$results_string .= '<h2 style="color:red">'.$_SESSION['results'].'</h2>';
  • 写回答

1条回答 默认 最新

  • douweng7308 2015-08-02 08:58
    关注

    You are defining results_string inside the if(!is_string($_SESSION['results'])) block. If that if ends up being false, results_string will not be defined in the else block. I'd suggest moving the following line before the if block:

    $results_string = '<table border=1><tr style="color:red; background-color: #b0c4de">';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)