2012-11-20 49 views
0

我試圖在schedule表中輸入員工編號(Emp_ID)30次。員工ID正從employee表中拉出。它將在第一個循環中工作,但在第二個循環中休息。我得到的錯誤是如何多次插入結果集?

"Fatal error: Call to a member function fetch_assoc() on a non-object in C:\wamp\www\server\roster\dates.php on line 110"

線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 . ']'); 
      } 
     }  
    } 
?> 

回答

3

$result已經在使用上while循環,您不能使用相同的名稱內環路,將覆蓋$result

變化的電流值$result不同的變量名$result_insert(我的意思後,插入查詢)

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

+0

不僅你在回答這個問題時變得很快,你已經死了!非常感謝! –

+0

我希望我一樣快:P –

1

您將要覆蓋$結果變量。在while循環中使用不同的變量名稱。

0

在if條件中,您使用的是相同的$ result變量,它將其類型更改爲非對象。把它的名字改成別的東西。