[英]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.