dqls67891 2017-09-28 03:29
浏览 69
已采纳

在循环外面的未定义变量 - PHP

I was wondering why I got an error saying Notice: Undefined variable: query_add_landdev_temp_payroll in C:\xampp\htdocs\op\ajax\ajaxLanddevNTPPayroll.php on line xx. for the past 2 weeks, it works perfect. When I tried to execute the code today, I got that kind of error. Here is my code:

$query_get_payroll = mysqli_query($new_conn, "SELECT ntp_with_ob_payroll_transaction.ntp_id, ntp_with_ob_payroll_transaction.allotment_code, ntp_with_ob_payroll_transaction.category_name, ntp_with_ob_payroll_transaction.block_number, ntp_with_ob_payroll_transaction.activity, ntp_with_ob_payroll_transaction.lot_number, ntp_with_ob_payroll_transaction.labor_cost, other_budget.quantity FROM ntp_with_ob_payroll_transaction JOIN other_budget ON ntp_with_ob_payroll_transaction.ob_id = other_budget.ob_id WHERE other_budget.transaction_id = $transaction_id AND other_budget.ob_number = '$ntp_number' AND is_payroll = 0");

while($row = mysqli_fetch_assoc($query_get_payroll)) {

    $ntp_id = $row['ntp_id'];
    $allotment_code = $row['allotment_code'];
    $category_name = $row['category_name'];
    $block_number = $row['block_number'];
    $activity = $row['activity'];
    $lot_number = $row['lot_number'];
    $labor_cost = $row['labor_cost']; //this is equivalent to unit cost
    $quantity = $row['quantity']; //this is equivalent to total percentage


    $query_add_landdev_temp_payroll = mysqli_query($new_conn, "INSERT INTO temp_payroll_landdev(ntp_id, userid, transaction_id, ntp_number, contractor_name, allotment_code, category_name, block_number, activity, lot_numbers, regular_labor, quantity) VALUES($ntp_id, $_SESSION[userid], $transaction_id, '$ntp_number', '$fullname', '$allotment_code', '$category_name', $block_number, '$activity', '$lot_number', '$labor_cost', '$quantity')");
}

if($query_add_landdev_temp_payroll) {

    echo 1;

    //database file name
    $database_file = $database.'.sql';
    $new_database_file = $new_database.'.sql';

    if(file_exists('backup/'.$new_database_file)) {

        unlink('backup/'.$new_database_file);

        //backup project database
        $command = "C:/xampp/mysql/bin/mysqldump --host=$new_host --user=$new_user --password=$new_pass $new_database > backup/$new_database_file";
        system($command);

    } else {

        //backup project database
        $command = "C:/xampp/mysql/bin/mysqldump --host=$new_host --user=$new_user --password=$new_pass $new_database > backup/$new_database_file";
        system($command);
    }
} else {

    echo 0;
}
  • 写回答

3条回答 默认 最新

  • dongraobei6719 2017-09-28 03:36
    关注

    If the query doesn't return any rows, mysqli_fetch_assoc() will return false the first time, so you'll never go into the loop, and never assign to the variable. If you then try to use the variable you get that warning.

    You can give it a default initial value before the loop, or you can change your test to:

    if (!empty($query_add_landdev_temp_payroll))
    

    BTW, the variable only contains the result of the INSERT in the last iteration of the loop. Maybe you should put the if block inside the loop, right after the INSERT?

    Also, there's no need to do INSERT in a loop. You can do the whole thing with a single query:

    INSERT INTO temp_payroll_landdev(ntp_id, userid, transaction_id, ntp_number, contractor_name, allotment_code, category_name, block_number, activity, lot_numbers, regular_labor, quantity)
    SELECT ntp_with_ob_payroll_transaction.ntp_id, ntp_with_ob_payroll_transaction.allotment_code, ntp_with_ob_payroll_transaction.category_name, ntp_with_ob_payroll_transaction.block_number, ntp_with_ob_payroll_transaction.activity, ntp_with_ob_payroll_transaction.lot_number, ntp_with_ob_payroll_transaction.labor_cost, other_budget.quantity 
    FROM ntp_with_ob_payroll_transaction 
    JOIN other_budget ON ntp_with_ob_payroll_transaction.ob_id = other_budget.ob_id 
    WHERE other_budget.transaction_id = $transaction_id AND other_budget.ob_number = '$ntp_number' AND is_payroll = 0
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)