简体   繁体   English

为什么两次调用相同的函数会导致php致命错误?

[英]why am i getting a php fatal error for calling the same function twice?

https://plnkr.co/edit/ZNlAyky7TzT4jknpnoDJ?p=preview https://plnkr.co/edit/ZNlAyky7TzT4jknpnoDJ?p=preview

here is a link to a plnkr with all my code written so far. 这是到目前为止我编写的所有代码的plnkr的链接。 I keep getting an 我不断得到

Fatal error: Cannot redeclare connect_to_db() (previously declared in /var/www/html/News/config/dbconnect.php:5) in /var/www/html/News/config/dbconnect.php on line 5 致命错误:无法在第5行的/var/www/html/News/config/dbconnect.php中重新声明connect_to_db()(先前在/var/www/html/News/config/dbconnect.php:5中声明)

the plunkr wont have the folder structure because i could not figure out how to add folders however here is my code for dbconnect.php plunkr不会具有文件夹结构,因为我不知道如何添加文件夹,但这是我的dbconnect.php代码

 <?php
        $pdo = null;
        function connect_to_db()
        {
            $dbengine   = 'mysql';
            $dbhost     = 'localhost';
            $dbuser     = 'root';
            $dbpassword = 'password';
            $dbname     = 'news';

            try{
                $pdo = new PDO("".$dbengine.":host=$dbhost; dbname=$dbname", $dbuser,$dbpassword);
                $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
                return $pdo;
            }
            catch (PDOException $e){
                $e->getMessage();
            }
        }

line 5 does not have a call to db connect so i dont know what is going on 第5行没有对db connect的调用,所以我不知道发生了什么

You are using 您正在使用

require __DIR__.'/dbconnect.php'

In both your Index.php and your functions.php, while requiring your functions.php in Index.php. 在Index.php和functions.php中,同时在Index.php中需要functions.php。

Therefore connect_to_db() is being defined twice. 因此, connect_to_db()被定义了两次。 Use require_once instead to prevent this: 请使用require_once来防止这种情况:

require_once __DIR__.'/dbconnect.php'

http://php.net/manual/en/function.require-once.php http://php.net/manual/zh/function.require-once.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在Laravel中遇到此错误:PHP Catchable致命错误? - Why am I getting this error with Laravel: PHP Catchable Fatal Error? 为什么我的代码出现PHP致命错误? - Why am I getting a PHP Fatal error with my code? 为什么在此php代码中出现致命错误: - Why am I getting Fatal error: in this php code? 为什么我的MongoDB函数出现致命错误? - Why am I getting a Fatal error with this MongoDB function? 错误在PHP中两次调用相同的功能 - error in calling same function twice in php 为什么在调用父代的构造函数时会出现致命错误? - Why am I getting Fatal error when calling a parent's constructor? PHP-为什么会出现此错误? 调用未定义的函数 - PHP - Why am I getting this error? Call to undefined function 为什么会出现此错误? 致命错误:找不到类“ TestCase” - Why am I getting this error? Fatal error: Class 'TestCase' not found 为什么会出现“致命错误:在非对象上调用成员函数fetch_assoc()”? - Why am I getting a “Fatal error: Call to a member function fetch_assoc() on a non-object”? 为什么会出现此Spreadsheet_Excel_Writer致命错误:使用writeFormula()的最大函数嵌套级别? - Why am I getting this Spreadsheet_Excel_Writer Fatal error: Maximum function nesting level with writeFormula()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM