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

当我将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>

此问题已经存在 这里有一个答案: p>

  • 参考 - 这个错误在PHP中意味着什么? 34答案 \ n span> li> ul> div>

    我使用PDO连接到MySQL,一切正常。 为了整理,我已经制作了一个包含PDO代码的包含文件。 这是完整的包含文件: p>

     &lt;?php 
     
     $ pdo = new PDO('mysql:host = localhost; dbname = ******  ','******',
    '******'); 
     $ pdo-&gt; setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION); 
     $ pdo-&gt;  exec('SET NAMES“utf8”'); 
     
    printf('hello'); 
    ?&gt; 
      code>  pre> 
     
     

    打印hello只是为了检查 文件被包含,它是。 问题是我现在加载页面时出现此错误 p>

     “致命错误:调用成员函数查询() 第9行上的/home3/danville/public_html/test/parkattractions.php中的非对象“
      code>  pre> 
     
     

    第9行是 $ result = $ pdo- &gt;查询($ query); code> p>

    当db-connect文件是主页的一部分时,它完全正常工作,它现在只使用它作为包含我 得到错误。 什么是rong,我该如何解决? p>

    编辑:这是获取错误的页面的最顶层: p>

       &lt;?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-&gt; query($ query); 
    } 
    catch(PDOException $ e)
     {
     $ output ='无法连接到 数据库服务器。'; 
     // include'output.html.php'; // 
    } 
      code>  pre> 
      div>

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.

You are including the file using its full URL. That means PHP should see the same thing that you see when you go to that URL. When you go to that URL, do you see PHP code (as PHP would expect) or an empty page?

If you see an empty page, PHP effectively includes an empty file and doesn't execute anything.

You should include using the server side path (like require_once '../db-connect.php';).