当我到达我的打印语句时,我从以下代码中得到了这个:
This is the for loop value : b
This is the for loop value : b
This is the for loop value :
This is NOT the for loop value :
我不确定$ le变量是怎么回事,为什么它在for循环中是一回事而在其中是另一回事。
foreach($this->letdowns AS $ld)
{
$le = "";
for($i = 0; $i <= sizeof($this->letdowns); $i++)
{
$le = $this->letdown_end[$i];
$msg = "This is the for loop value : " . print_r($le, true) . "<BR>";
print $msg;
}
$msg = "This is NOT the for loop value : " . print_r($le, true) . "<BR>";
print $msg;
//$query = "INSERT INTO letdown_events (letdown_line_id, operation_id) "
$query = "INSERT INTO letdown_events (letdown_line_id, letdown_end, operation_id) "
//."values (".$ld->get('id').",".$this->id.")";
."values (".$ld->get('id').",".$le.",".$this->id.")";
// Make sure this query completes, otherwise rollback the transaction
var_dump($query);
if(!$result = mydb::cxn()->query($query))
{
throw new Exception('An error occurred while trying to create a letdown event for Letdown Line #'.$ld->get('id').': '.mydb::cxn()->error);
}
}
当我var_dump变量时,它也是空白的。
string(103) "INSERT INTO letdown_events (letdown_line_id, letdown_end, operation_id) values (1552020000,,1781020000)"
任何人都可以请指出为什么我不能在循环外使用$ le变量,我只是想找到一种使用该查询将其插入数据库的好方法。
答案 0 :(得分:3)
for($i = 0; $i < sizeof($this->letdowns); $i++)
不是
for($i = 0; $i <= sizeof($this->letdowns); $i++)
但为什么不简单地使用
foreach($this->letdowns as $le)