2011-03-15 48 views
22

我收到以下錯誤:「意外T_OBJECT_OPERATOR」錯誤在PHP

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in.. on line 52. 

52號線是if ($result = mysqli->query...。如果我註釋掉該行,則在$mysqli->query("INSERT INTO...上發生同樣的錯誤。

爲什麼這會給出錯誤?

$unique_code = ""; 
$inserted = false; 
while(!$inserted) { 
    $unique_code = generateCode(); 
    echo $unique_code; 

    // Check if it exists 
    if ($result = mysqli->query("SELECT unique_code FROM coming_soon_emails WHERE unique_code = '$unique_code'")) { 
     // Check no record exists 
     if ($result->num_rows == 0) { 
      // Create new record 
      $mysqli->query("INSERT INTO coming_soon_emails (email,unique_code) VALUES ('" . $mysqli->real_escape_string($_POST['email']) . "','$unique_code')"); 
      // Set inserted to true to ext loop 
      $inserted = true; 
      // Close the result object 
      $result->close(); 
     } 
    } else { 
     // Quit if we can't check the database 
     die('Something went wrong with select'); 
    } 
} 

回答

27

您忘記了$mysqli之前的美元符號。