0

Emp_ID従業員 ID ( ) をscheduleテーブルに 30 回入力しようとしています。従業員 ID がemployeeテーブルから取得されています。最初のループでは機能しますが、2 番目のループでは機能しません。私が得るエラーは

「致命的なエラー: 110 行目の C:\wamp\www\server\roster\dates.php の非オブジェクトに対するメンバー関数 fetch_assoc() の呼び出し」

110行目はwhileループです。結果セットが空になっているためにこれが起こっているとしか思えませんが、修正方法がわかりません。

<?php
//Select all of the current Employees by ID number
$sql = ("SELECT Emp_ID FROM employee");

//Run a check on the query to make sure it worked.
//if it failed then print the error.
if(!$result = $mysqli->query($sql))
{
die('There was an error getting the Emp_ID from the employee table [' . $mysqli->error . ']');
}

//Loop through the results...
while($row = $result->fetch_assoc())
    {
    //...and for each employee ID, enter it into the table 30 times.
    for($i = 1; $i <= 30; $i++ )
        {
        $sql = ("INSERT INTO schedule (Emp_ID) VALUES ('" . $row['Emp_ID'] . "')");

            //Run a check on the query to make sure it worked.
            //if it failed then print the error.
            if(!$result = $mysqli->query($sql))
            {
            die('There was an error inserting the Emp_ID into the schedule [' . $mysqli->error . ']');
            }
        }       
    }
?>
4

3 に答える 3

3

$resultはすでにwhileループで使用されており、現在の値を上書きするループ内で同じ名前を使用することはできません$result

$resultを別の変数名に変更します$result_insert(つまり、挿入クエリの後)

if(!$result_insert = $mysqli->query($sql))

于 2012-11-20T11:38:53.053 に答える
1

$result 変数を上書きしています。while ループで別の変数名を使用します。

于 2012-11-20T11:39:52.957 に答える
0

if 条件内では、同じ $result 変数を使用しており、その型を非オブジェクトに変更しています。その名前を別のものに変更します。

于 2012-11-20T11:45:43.713 に答える