php函数redeclare致命错误

时间:2013-10-22 12:50:46

标签: php function

我收到以下错误:

Fatal error: Cannot redeclare register() (previously declared in    
C:\xampp\htdocs\spikes\functions.php:2) in C:\xampp\htdocs\spikes\functions.php on line 20

这很奇怪,因为我宣布它只是一次,但错误说我重新宣布了它。

<?php
function register($username,$password,$email,$firstname,$lastname)
{
    $con=connect();
    // Check connection
    if (mysqli_connect_errno())
      echo "Failed to connect to MySQL: " . mysqli_connect_error();

    $date = date("Y/m/d");
    mysql_set_charset('utf8');
    $sql="INSERT INTO userlist (username, password, email, firstname, lastname, joined)
    VALUES ('$username', '$password', '$email', '$firstname', '$lastname', '$date')";

    if (!mysqli_query($con,$sql))
      die('Error2: ' . mysqli_error($con));

    mysqli_close($con);
    $id = getIdByUserName($username);
    createSession($id,$firstname);
}

function check($username,$password,$email,$firstname,$lastname)
{
    $error = "";
    $con=connect();
    if (mysqli_connect_errno())
      echo "Failed to connect to MySQL: " . mysqli_connect_error();

    $result = mysqli_query($con,"SELECT * FROM userlist WHERE username='".$username."'");
    $num_rows = mysqli_num_rows($result);
    if ($num_rows>0) 
        $error = "*השם משתמש קיים במערכת";

    $result = mysqli_query($con,"SELECT * FROM userlist WHERE email='".$email."'");
    $num_rows = mysqli_num_rows($result);
    if ($num_rows>0) 
    {
        if(strlen($error)>0)
            $error = $error."<br>"."*האימייל קיים במערכת";
        else
            $error = "*האימייל קיים במערכת";
    }


    if(strlen($error)<1)
        register($username,$password,$email,$firstname,$lastname);

    mysqli_close($con);
    return $error;
}
function connect()
{
    require ('config.php');
    return mysqli_connect($host,$user,$password,$database);
}
function createSession($userid,$firstname)
{
    $expire=time()+60*60*24*30;
    setcookie("usid", $userid, $expire);
    setcookie("usname", $firstname, $expire);
}
function getIdByUserName($username)
{
    $con=connect();
    if (mysqli_connect_errno())
      echo "Failed to connect to MySQL: " . mysqli_connect_error();

    $result = mysqli_query($con,"SELECT id FROM userlist WHERE username='".$username."'");
    $row = mysqli_fetch_array($result);
    $id = $row['id'];
    return $id;
}
function getNameByUserName($username)
{
    $con=connect();
    if (mysqli_connect_errno())
      echo "Failed to connect to MySQL: " . mysqli_connect_error();

    $result = mysqli_query($con,"SELECT firstname FROM userlist WHERE username='".$username."'");
    $row = mysqli_fetch_array($result);
    return $row['firstname'];
}
function post($type, $brand, $gender, $size, $hand, $isNew, $price, $desc, $imgname1, $imgname2, $name, $userid, $phone)
{
    $con=connect();
    // Check connection
    if (mysqli_connect_errno())
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    $date = date("Y/m/d");
    mysql_set_charset('utf8');
    $sql="INSERT INTO spikes (type, brand, gender, size, hand, new, price, description, imgname1, imgname2, date, name, userid, phone)
    VALUES ('$type', '$brand', '$gender', '$size', '$hand', '$isNew', '$price', '$desc', '$imgname1', '$imgname2', '$date', '$name', '$userid', '$phone')";

    if (!mysqli_query($con,$sql))
      {
      die('Error3: ' . mysqli_error($con));
      }
    mysqli_close($con);
}
function getFieldById($userid,$field)
{
    $con=connect();
    if (mysqli_connect_errno())
      echo "Failed to connect to MySQL: " . mysqli_connect_error();

    $result = mysqli_query($con,"SELECT $field FROM userlist WHERE id=$userid ");
    $row = mysqli_fetch_array($result);
    return $row[$field];
}
function getActivePosts()
{
    $con=connect();
    if (mysqli_connect_errno())
      echo "Failed to connect to MySQL: " . mysqli_connect_error();

    $result = mysqli_query($con,"SELECT * FROM spikes");
    $num_rows = mysqli_num_rows($result);

    mysqli_close($con);
    return $num_rows;
}
function getUserActivePosts($id)
{
    $con=connect();
    if (mysqli_connect_errno())
      echo "Failed to connect to MySQL: " . mysqli_connect_error();

    $result = mysqli_query($con,"SELECT * FROM spikes WHERE userid=$id");
    $num_rows = mysqli_num_rows($result);

    mysqli_close($con);
    return $num_rows;
}
function getAmountRegistered()
{
    $con=connect();
    if (mysqli_connect_errno())
      echo "Failed to connect to MySQL: " . mysqli_connect_error();

    $result = mysqli_query($con,"SELECT * FROM userlist");
    $num_rows = mysqli_num_rows($result);

    mysqli_close($con);
    return $num_rows;
}
?>

我已经附加了其他函数,因为我在寄存器函数中使用了其中一些函数。

**编辑

我从这个来源只包含一次文件:

<?php
if(isset($_POST["username"]))
{
    include('functions.php');
    $error = check($_POST["username"],$_POST["password"],$_POST["email"],$_POST["firstname"],$_POST["lastname"]);
    if(strlen($error) > 0)
    {
        echo $error;
    }
    else
    {
        echo "XXX";
    }
}
?>

5 个答案:

答案 0 :(得分:2)

每次调用

include() :时都会包含文件

include_once() :在整个文档中包含一次文件

require():当文件必不可少时使用,类似于include()

require_once():一次用于整个文档

include_once()&amp; require_once()以避免重复

include()&amp; require()创建重复的

按照错误,PHP提供完整且最佳的错误报告,以了解错误发生位置的问题。

答案 1 :(得分:1)

您包含/要求文件两次。第20行是函数声明的结束}。

答案 2 :(得分:0)

这意味着您已经拥有一个名为register()的函数。

也许你两次包含同一个文件或发生了一次怪异的copypasta事故。检查您的代码是否有任何双重功能。

答案 3 :(得分:0)

请尝试使用 include_once() require_once() 以避免重复。

答案 4 :(得分:0)

将您的需求对帐单更改为require_once "<file>";

您还可以将注册表包装在以下代码中,以缓解您的紧急问题:

if(!function_exists("register")) {
    function register($username,$password,$email,$firstname,$lastname) {
        // contents here
    }
}

我敢打赌,您可能会在functions.php内加入config.php