简体   繁体   English

注意:使用未定义的常量 DB_HOST - 在 C:\\xampp\\htdocs\\blog\\system\\functions.php 中第 31 行假定为“DB_HOST”

[英]Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in C:\xampp\htdocs\blog\system\functions.php on line 31

Getting a couple of errors and can't for the life of me see where I am falling down.犯了几个错误,我一辈子都看不到我跌倒的地方。 Below is the functions file下面是函数文件

<?php
include('config.php');

function getAllPosts()
{
    try {
        $dbh = new PDO(DB_HOST, DB_USER, DB_PASS);
    } catch (PDOException $e) {
        echo $e->getMessage();
    }

    $stmt = $dbh->prepare('SELECT id, title, content FROM posts ORDER BY created_at DESC');
    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $results;
}

function getSinglePost($id)
{
    try {
        $dbh = new PDO(DB_HOST, DB_USER, DB_PASS);
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
    $stmt = $dbh->prepare('SELECT title, content FROM posts WHERE id = ?');
    $bindings = array($id);
    $stmt->execute($bindings);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    return $result;
}

?>

Also figured I should include the page that I am running to generate the error还认为我应该包含我正在运行的页面以生成错误

<?php include('system/functions.php'); ?>
<html>
<head>
<title>Create A New Post | My First Blog</title>

<link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>
<div id="form">
<?php if (isset($_GET['id'])){ ?>

<h2>Single Post:</h2>

<?php $post = getSinglePost($_GET['id']); ?>
<?php print_r($post); ?>

<?php } ?>
<fieldset>
</fieldset>
</div>

</body>
</html>

Any help much appreciated these are the errors in full.非常感谢任何帮助,这些都是完整的错误。

Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in C:\\xampp\\htdocs\\blog\\system\\functions.php on line 31注意:使用未定义的常量 DB_HOST - 在 C:\\xampp\\htdocs\\blog\\system\\functions.php 中第 31 行假定为“DB_HOST”

Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in C:\\xampp\\htdocs\\blog\\system\\functions.php on line 31注意:使用未定义的常量 DB_USER - 在第 31 行的 C:\\xampp\\htdocs\\blog\\system\\functions.php 中假定为“DB_USER”

Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in C:\\xampp\\htdocs\\blog\\system\\functions.php on line 31 invalid data source name Notice: Undefined variable: dbh in C:\\xampp\\htdocs\\blog\\system\\functions.php on line 37注意:未定义常量 DB_PASS 的使用 - 假设 'DB_PASS' in C:\\xampp\\htdocs\\blog\\system\\functions.php on line 31 invalid data source name 注意:未定义变量:dbh in C:\\xampp\\htdocs\\blog\\第 37 行的 system\\functions.php

Fatal error: Call to a member function prepare() on a non-object in C:\\xampp\\htdocs\\blog\\system\\functions.php on line 37致命错误:在第 37 行的 C:\\xampp\\htdocs\\blog\\system\\functions.php 中的非对象上调用成员函数 prepare()

Should include config file too也应该包含配置文件

<?php
define('DB_HOST','mysql:host=localhost;dbname=blog');
define('DB_USER','root');
define('DB_PASS','');
?>

The constants you are using for Host , User and Password haven't been defined yet.您用于HostUserPassword的常量尚未定义。 There's probably something wrong with your config.php .您的config.php可能有问题。

that means DB_HOST is not defined or DB_HOST is not accessible in the current context.这意味着DB_HOST未定义或DB_HOST在当前上下文中不可访问。

Try place these varibles in the script at the begining instead of include('config.php');尝试将这些变量放在脚本的开头而不是include('config.php');

define('DB_HOST','mysql:host=localhost;dbname=blog');
define('DB_USER','root');
define('DB_PASS','');

Use particular terms in place of reference...使用特定术语代替参考...

For eg例如

$dbh = new PDO(DB_HOST, DB_USER, DB_PASS);

Change to...改成...

$dbh = new PDO('localhost', 'root', '')

That would work.那行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 注意:未定义的变量:第7行的C:\\ wamp \\ www \\ cbmall \\ index.php中的db_host - Notice: Undefined variable: db_host in C:\wamp\www\cbmall\index.php on line 7 常量 DB_HOST 已定义 - Constant DB_HOST already defined PHP 错误 - 常量 DB_HOST 已定义? - PHP error - Constant DB_HOST already defined? 未知的 MySQL 服务器主机 &#39;DB_HOST&#39; (0) - Unknown MySQL server host 'DB_HOST' (0) 注意:使用未定义的常量tourid-在47行的C:\\ xampp \\ htdocs \\ Cricket Score Board \\ displayresults.php中假定为&#39;tournamentid&#39; - Notice: Use of undefined constant tournamentid - assumed 'tournamentid' in C:\xampp\htdocs\Cricket Score Board\displayresults.php on line 47 如何使用if(&#39;DB_HOST&#39;,&#39;localhost&#39;) - how to work with if define('DB_HOST','localhost') 使用DB_HOST = localhost和127.0.0.1 PDOException运行Laravel 5项目 - Running Laravel 5 project with DB_HOST=localhost and 127.0.0.1 PDOException Laravel 5.5如何根据要求更改DB_HOST - Laravel 5.5 How to change DB_HOST on request 如何在Laravel中获取当前查询的DB_HOST值 - How to get the current query DB_HOST value in Laravel 尝试创建用于连接数据库的字符串时出错(mysql:host =&#39;。DB_HOST。&#39;; dbname =&#39;。DB_DATABASE;) - Errors when trying to create the string for connecting to a Database (mysql:host=' . DB_HOST . ';dbname=' . DB_DATABASE;)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM