2012-11-20 52 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变量,它将其类型更改为非对象。把它的名字改成别的东西。