doulangbizhan5160 2013-08-11 00:03 采纳率: 100%
浏览 37
已采纳

当我将PDO数据库连接详细信息放入包含文件时,我收到错误[重复]

This question already has an answer here:

I connect to MySQL using a PDO and all works fine. To tidy things up I have made an include file that contains the PDO code. This is the full include file:

<?php

$pdo = new PDO('mysql:host=localhost;dbname=******', '******',
'******');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');

printf('hello');
?>

The print hello was just to check the file was being included, which it is. The problem is I now get this error when loading the page

"Fatal error: Call to a member function query() on a non-object in /home3/danville/public_html/test/parkattractions.php on line 9"

Line 9 is $result = $pdo->query($query);

It was all working fine when the db-connect file was part of the main page, it's only now using it as an include I get the error. What has gone rong and how do I fix it?

EDIT: This is the very top of the page getting the error:

<?php
include 'http://www.themeparkfocus.com/db-connect.php';

try
{
$park_id = $_GET['park_id'];
$query="SELECT * FROM tpf_parks WHERE park_id = $park_id";
$result = $pdo->query($query);
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
//include 'output.html.php';//
}
</div>
  • 写回答

2条回答 默认 最新

  • dougou7782 2013-08-11 00:12
    关注

    Use a file path in your include, not the url. If you use an url, at best the url is requested and the contents downloaded, but then it will probably a generated page and not a php source file.

    Just open http://www.themeparkfocus.com/db-connect.php in your browser, and you'll see what you include: no PHP code.

    Also, use require if you need the file. You will get a clear error message if including the file/url fails, although it probably won't help you in this case, since the url is valid.

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

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题