doq13207 2012-05-11 22:31
浏览 45
已采纳

PHP - 使用文件外部的函数创建它

I have 3 php files. The first (connexion.php) one contains a function I use to create a new PDO connexion:

<?php
header('Content-Type: text/html; charset=utf-8');
date_default_timezone_set('Europe/Paris');

function connexion($host, $user, $pass, $db){
    $db_host = $host;  
    $db_user = $user;  
    $db_password = $pass;  
    $db_database = $db;               
    return $connexion = new PDO("mysql:host=$db_host;dbname=$db_database;charset=utf8", $db_user, $db_password);
}

?>

The second file (my_function.php) contains a function that contains only one function that connects to a db and then is echoing some information.

    <?php
    include(connexion.php);
    function my_first_function(){
        try{
           $connexion = connexion('localhost', 'user', 'user', 'mydb');
           $connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
           //some code that echoes information
        }
        catch(...){...}
    }
    ?>

My problem is when I try to include that last file into my third php script in order to be abble to use the function. The script is not even launching. I just have 'internal error'. If I remove the include(my_function.php) my file is loaded properly. I don't get it. Hope someone can help me understand. Thanks in advance. Cheers. Marc. Here below the third file:

<?php
include('connexion.php');
include('my_function.php');

//some code
my_first_function();
?>
  • 写回答

4条回答 默认 最新

  • dongyi6543 2012-05-11 22:54
    关注

    You should not use include('connexion.php'); in the third file as it will also be included already automatically when you do include('my_function.php');

    As it contains a function declaration, that will lead to an error because that function has already been declared.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看