解析错误:语法错误,第65行的......意外T_CATCH?

时间:2011-03-16 01:44:07

标签: php mysql

错误发生在第65行:} catch(Exception $ e){

<?php

require "includes/connect.php";



function generateCode($length = 5)
{
    $characters = 'bcdfghjkmnpqrstvwxyz';
    $string = '';

    for ($i = 0; $i < $length; $i++) {
        $string .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $string;
}


$msg = '';


if($_POST['email']) {

    // Requested with AJAX:
    $ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest');


    try {

        if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
            throw new Exception('Invalid Email!');
        }

        if($ajax){
            die('{"status":1}');
        }

        $unique_code = "";
        $inserted = false;

        // Keep looping until we've inserted a record
        while(!$inserted) {

            // Generate a code
            $unique_code = generateCode();

            // 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();
            }
        }

    } catch (Exception $e){

        if($ajax){
            die(json_encode(array('error'=>$e->getMessage())));
        }

        $msg = $e->getMessage();
        die($msg);
    }      

} else {
    // Quit if we can't check the database
    die('Something went wrong with select');
}   



?>

1 个答案:

答案 0 :(得分:3)

你的牙套不匹配。 你的while循环末尾没有正确的匹配括号(在两个if语句之后)...只需在catch行之前添加}

你应该使用Notepad ++来编写PHP!