【问题标题】:I'm unable to execute same function with multiple times in php我无法在php中多次执行相同的功能
【发布时间】:2016-09-30 05:40:07
【问题描述】:

这是我的 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;
        }
    }
}

当我使用$lastname=getinfo('lastname'); after using $firstname=getinfo('firstname'); 时,在上面的代码中(在 index.php 中)。它给了我以下错误。 <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>

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

【问题讨论】:

标签: php mysql function mysqli login-script


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我猜你有 javascript 开发背景。

    但是在 php 中我们声明函数的方式不同。

    所以例如你的 core.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 mysqli_result($res, $row, $f) {
        $res->data_seek($row); 
        $datarow = $res->fetch_array(); 
        return is_array($datarow)?$datarow[$f]: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)){
            if($query_result=mysqli_result($query_run,0,$field)){
                return $query_result;
            }
        }
    }
    

    现在我们知道我们在core.php 中声明了mysqli_result 函数

    所以如果任何 php 文件将 require_once 'core.inc.php'; 我们不需要重新声明这个函数。在index.php 的情况下,您调用include_once 'login.php';,在login.php 中,您试图重新声明mysqli_result,这是多余的。所以你的login.php 片段可以转换为:

     else if($query_num_rows == 1){
         $user_id= mysqli_result($query_run, 0,'id');
         $_SESSION['user_id'] = $user_id;
         header('Location: index.php');
     }
    

    我猜你只是 php 的初学者,其他开发人员可以提供一些其他建议。但我的回答只是关于你的问题,为什么你有错误 Cannot redeclare mysqli_result().

    【讨论】:

      【解决方案3】:

      为了所有的好处,请使用 PDO

      $query = "SELECT id FROM login WHERE username=? AND password=?";
      $stmt  = $pdo->prepare($query);
      $stmt->execute([$username, $password]);
      $user_id = $stmt->fetchColumn();
      if($user_id) {
          $_SESSION['user_id'] = $user_id;
          header('Location: index.php');
          exit;
      } else {
          echo 'You have entered wrong username or password !';
      }
      

      这个。是。全部。 您需要的代码。这几行而不是你所拥有的所有可怕的混乱,试图重新动画一个死的 mysql_result 函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多