php - 我无法在 php 中多次执行相同的函数

标签 php mysql function mysqli login-script

这是我的 login.php 代码

require_once 'connect.inc.php';
require_once 'core.inc.php';

if(isset($_POST['username']) && isset($_POST['password'])){
    $username=$_POST['username'];
    $password=$_POST['password'];
    $password_hash=md5($password);
    if(!empty($username) && !empty($password)){
        $query="SELECT `id` FROM `login` WHERE `username`='$username' AND `password`='$password_hash'";
        if($query_run = mysqli_query($stat,$query)){
            $query_num_rows = mysqli_num_rows($query_run);
            if($query_num_rows == 0){
                echo 'You have entered wrong username or password !';
            }
            else if($query_num_rows == 1){
                function mysqli_result($res, $row, $field) {
                    $res->data_seek($row); 
                    $datarow = $res->fetch_array(); 
                    return $datarow[$field]; 
                }
                $user_id= mysqli_result($query_run, 0,'id');
                $_SESSION['user_id'] = $user_id;
                header('Location: index.php');
            }
        }
        else
        {
            echo 'You have entered wrong username or password !';
        }
    }else {
        echo 'You must have to enter username and password';
    }
}
<form action="<?php echo $server; ?>" method="POST">
    Username:
    <input type="text" placeholder="Enter the userid" name="username">
    Password:
    <input type="password" placeholder="Enter your password" name="password">    
    <input type="submit" value="Log in">

这是index.php的代码

require_once 'core.inc.php';
require_once 'connect.inc.php';

if(get_status()){
$firstname=getinfo('firstname');
$lastname=getinfo('lastname');
echo 'You are logged in ,'.$firstname.' '.$lastname.'';
echo "<br><a href='logout.php'>Logout</a><br>";
}
else {
 include_once 'login.php';
}

这是 core.inc.php 的代码

session_start(); 
ob_start();
$server =$_SERVER['SCRIPT_NAME'];
@$ref_url=$_SERVER['HTTP_REFERER'];
function get_status(){
    if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])){
        return true;
    }
    else
        return false;  
}

function getinfo($field){
    $get_query="SELECT `$field` FROM `login` WHERE `id`='".$_SESSION['user_id']."'";
    require 'connect.inc.php';
    if($query_run=mysqli_query($stat,$get_query)){
            function mysqli_result($res, $row, $f) {
            $res->data_seek($row); 
            $datarow = $res->fetch_array(); 
            return $datarow[$f]; 
        }
        if($query_result=mysqli_result($query_run,0,$field)){
            return $query_result;
        }
    }
}

在上面的代码(在index.php中)当我使用 $lastname=getinfo('lastname'); after using $firstname=getinfo('firstname'); 时。它给了我以下错误。 <b>Fatal error:</b> <i>Cannot redeclare mysqli_result() (previously declared in C:\xampp\htdocs\good\core.inc.php:18) in C:\xampp\htdocs\good\core.inc.php on line 18</i>

请有人帮助我。我是第一次学习这种语言。

最佳答案

在函数 getinfo 之外定义函数 mysqli_result。然后从 getinfo 调用它。

由于您已经在getinfo中定义了该函数,因此mysqli_result的函数定义将在第一次调用getinfo时执行。当第二次调用getinfo时,mysqli_result的函数定义将再次执行。但它已经被定义了,你会得到这个错误。

关于php - 我无法在 php 中多次执行相同的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37558745/

相关文章:

c++ - 如何在 C++ 中正确关闭我的程序?

matlab - 抑制函数的命令窗口输出

c - 在c中的字符串左侧添加空格

php POSTstream_context_create 和 file_get_contents

php - Laravel 社交名媛 : InvalidStateException (sometimes)

php - 如何正确解析 PHP 中的整数?

php - 如何在上传到数据库之前在php中更改图像文件名

python - 从 MySQL 研究中查找 python 中的子字符串

mysql - perl DBI 中的占位符使用

php - 在远程服务器上安装 Xdebug 以进行分析