[英]I'm new to PHP and I don't understand why I'm getting this undefined variable error
This is the relevant part of my code and the variable issue arises with results_string.这是我的代码的相关部分,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 [Sun Aug 02 04:29:47.726659 2015] [:error] [pid 8108:tid 748] [client ::1:63388] PHP 注意:C:\\Apache24\\htdocs\\Prototype\\New Working Prototype 中的未定义变量:results_string \\QueryResults.php 第 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>';
You are defining results_string
inside the if(!is_string($_SESSION['results']))
block.您正在if(!is_string($_SESSION['results']))
块中定义results_string
。 If that if
ends up being false, results_string
will not be defined in the else
block.如果if
最终为 false,则results_string
将不会在else
块中定义。 I'd suggest moving the following line before the if block:我建议在 if 块之前移动以下行:
$results_string = '<table border=1><tr style="color:red; background-color: #b0c4de">';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.