dtol41388 2016-04-21 15:20
浏览 46

致命错误:未定义的函数db()(但它是)

So I have this pdo.php file that contains my Database connection. This is the function:

function db() {
    static $dbh;
    if(!isset($dbh)) {
        $dsn = sprintf('mysql:host=%s;dbname=%s', SHOP_DB_HOST, SHOP_DB_NAME);
        try {
            $dbh = new PDO($dsn, SHOP_DB_USER, SHOP_DB_PASSWORD);
        } catch(PDOException $e) {
            header('Status: 500 Internal Server Error');
            echo '<b>Error:</b> Database connection failed.';
            exit;
        }
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $dbh->exec('SET NAMES utf8');
    }
    return $dbh;
}

And this is another file in which I need to use db():(It's detecting an ajax call and based on that, supposed to update information)

if(isset($_POST['updateTree'])) {
    $page_id = $_POST['page_id'];
    $parent_id = $_POST['parent_id'];
    $customer_id = $_SESSION['customer_id'];
    $stojkovQ = db()->prepare("
        UPDATE 
            cms_pages
        SET 
            topicId = :parent_id
        WHERE
            page_id = :page_id
        AND
            customer_id = :customer_id
    ");
    $stojkovQ->execute(array(
        'customer_id'    => $customer_id,
        'page_id'        => $page_id,
        'topicId'        => $parent_id
    ));
    echo 'updated';
}

So after after getting an "Fatal Error: Call to undefined function db()" even tho it's included in my header.php file(containing all the includes and config information for my site). If I try to include it again just above my query it yields another error(Cannot redeclare db())

Any lead towards the resolving of this problem will be of great help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?