无法重新声明我的功能php

时间:2015-06-30 17:57:03

标签: php function mysqli

我使用Linux Debian

本地灯服务器

我试图在数据库和php&之间建立连接。制作API:

db.php(连接文件!):

<?php
$tf_host = 'localhost' ; 
$tf_dbname = 'tinyforum' ; 
$tf_username = 'root';
$tf_password = '976431' ;

$tf_handle = mysqli_connect($tf_host , $tf_username , $tf_password,$tf_dbname);



if (!$tf_handle){   
    die('connection problem ..');
}

$tf_db_result = mysqli_select_db($tf_handle,$tf_dbname);


mysqli_query($tf_handle, "SET NAMES 'utf8'") ;

function tinyf_db_close() {
    global $tf_handle;
    mysqli_close($tf_handle);


}

?>

我试图运行此文件===&gt;

<?php


error_reporting(E_ALL);
ini_set('display_errors', 1);

if(!isset($_POST['username']) or (!isset($_POST['password'])) or (!isset($_POST['email']))) {
    die('bye');
}


require_once('db.php');
require_once ('usersAPI.php');

$result = tinyf_users_add($_POST['username'],$_POST['password'],$_POST['email'], 0);
tinyf_db_close();
if($result) {
    die('yes');
}
else {
    die('no');
}

我收到错误:

  

致命错误:无法在第21行的/var/www/html/tinyforum/db.php中重新声明tinyf_db_close()(之前在/var/www/html/tinyforum/db.php:21中声明)

usersAPI.php的代码

<?php 
//Users APIs
function tinyf_users_get($extra ='')
{
    global $tf_handle;
    $query = sprintf("SELECT * FROM `users` %s",$extra );
    $qresult = mysqli_query($tf_handle, $query);

    if (!$qresult)
        return NULL;
    $recount = mysqli_num_rows($qresult);
    if ($recount == 0)
        return NULL ;
    $users = array();
    for($i = 0 ; $i < $recount ; $i++)
        $users[count($users)] = mysqli_fetch_object($qresult);
    //mysql_free_result($qresult);

    return $users;
}

function tinyf_users_get_by_id($uid)
{
    $id = (int)$uid;
    if($id == 0 )
        return NULL ;
    $result = tinyf_users_get('WHERE id ='.$id);
    if($result == NULL)
        return NULL;
    $user = $result[0];
    return $user;
}

function tinyf_users_add($name,$password,$email,$isadmin)
{
    global $tf_handle;
    if ((empty($name)) || (empty($password)) || (empty($email)))
            return false;

    $n_email = mysqli_real_escape_string($tf_handle, strip_tags($email));
    if(!filter_var($n_email,FILTER_VALIDATE_EMAIL))
        return false;   

    $n_name = mysqli_real_escape_string($tf_handle, strip_tags($name));
    $n_pass = md5(mysqli_real_escape_string($tf_handle, strip_tags($password)));
    $n_isadmin = (int)$isadmin;
    if(($n_isadmin != 0) && ($n_isadmin != 1))
        $n_isadmin = 0 ; 
    $query = sprintf("INSERT INTO `users` VALUE(NULL,'%s','%s','%s','%d')",$n_name,$n_pass,$n_email,$n_isadmin);

    $qresult = mysqli_query($tf_handle, $query);
    if(!$qresult)
        return false;
    return true;
}

function tinyf_users_delete($uid)
{
    global $tf_handle;
    $id =  (int)$uid;
    if($id == 0 )
        return false ;  

    $query   = sprintf ("DELETE FROM `users` WHERE `id`= %d",$id);

    $qresult = mysqli_query($tf_handle, $query);
    if(!$qresult)
        return false;

    return true;

}

function tinyf_users_update($uid,$name = NULL,$password = NULL ,$email = NULL,$isadmin = -1)
{
    global $tf_handle;
    $id =  (int)$uid;
    if($id == 0 )
        return false ;
    $n_isadmin = (int)$isadmin ; // if 0 = FALSE not int ,,, if int  = 1 
    $user = tinyf_users_get_by_id($id);
    if(!$user)  
        return false;
    if ((empty($name)) && (empty($password)) && (empty($email)) && ($user -> isadmin == $n_isadmin))
        return false;   
    $fields = array() ;
    $query = 'UPDATE `users` SET ' ;

    if(!empty($email))
        {
            $n_email = mysqli_real_escape_string($tf_handle, strip_tags($email));
            if(!filter_var($n_email,FILTER_VALIDATE_EMAIL))
                return false;

            $fields[count($fields)] = "`email` = '$n_email'";
        }   

    if(!empty($name))
        {
            $n_name = mysqli_real_escape_string($tf_handle,strip_tags($name));
            $fields[count($fields)] = "`name` = '$n_name'";
        }
    if(!empty($password))
        {
            $n_pass = md5(mysqli_real_escape_string($tf_handle, strip_tags($password)));
            $fields[count($fields)] = "`password` = '$n_pass'";
        }       

    if($n_isadmin == -1)
    {
        $n_isadmin = $user -> isadmin  ;
    }
    elseif ($n_isadmin > 1)
    {
        $n_isadmin = $user -> isadmin ;
    }

    $fields[count($fields)] = "`isadmin` = $n_isadmin";

    $fcount = count($fields);

    if($fcount == 1)
     {
        $query .= $fields[0].'WHERE `id`= ' .$id ; //   $query = 'UPDATE `users` SET '
        $qresult = mysqli_query($tf_handle, $query);
        if(!qresult)
            return false;     
        else
            return true;


     }  
    for($i = 0; $i < $fcount ; $i++)
     {
        $query .= $fields[$i];
        if($i != ($fcount - 1)) // i = 0 that the first element in the array .. 2 will be - 1 last 3shan hwa by3ed el array mn wa7ed :D
            $query .=' , ';
     }

     $query .= ' WHERE `id` = '.$id;

     $qresult = mysqli_query($tf_handle, $query);
     if(!$qresult)
        return false;
     else
        return true;

}



include ('db.php') ;
error_reporting(E_ALL);
ini_set('display_errors', 1);
$result = tinyf_users_update(6,'ggrgrgr','123456',NULL,0);
if($result) //($uid,$name = NULL,$password = NULL ,$email = NULL,$isadmin = 0)
    echo 'success';


?>

1 个答案:

答案 0 :(得分:0)

你有两个选择

  1. 从您的主文件中删除require_once('db.php');,因为您已将其包含在usersAPI.php
  2. 中 如果您打算同时包含require_once('db.php');usersAPI.php
  3. db.php删除usersAPI.php