注意:使用未定义的常量DB_HOST-在第31行的C:\ xampp \ htdocs \ blog \ system \ functions.php中假定为'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.
注意:使用未定义的常量DB_HOST-在第31行的C:\ xampp \ htdocs \ blog \ system \ functions.php中假定为'DB_HOST'
Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' 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_USER - assumed 'DB_USER' in C:\xampp\htdocs\blog\system\functions.php on line 31
注意:使用未定义的常量DB_PASS-在第31行的C:\ xampp \ htdocs \ blog \ system \ functions.php中假定为'DB_PASS' 无效的数据源名称 注意:未定义变量:第37行的C:\ xampp \ htdocs \ blog \ system \ functions.php中的dbh
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
致命错误:在第37行的C:\ xampp \ htdocs \ blog \ system \ functions.php中的非对象上调用成员函数prepare()
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\blog\system\functions.php on line 37
也应该包含配置文件
<?php
define('DB_HOST','mysql:host=localhost;dbname=blog');
define('DB_USER','root');
define('DB_PASS','');
?>
推荐答案
尚未定义用于Host
,User
和Password
的常量.您的config.php
可能有问题.
The constants you are using for Host
, User
and Password
haven't been defined yet. There's probably something wrong with your config.php
.
这篇关于注意:使用未定义的常量DB_HOST-在第31行的C:\ xampp \ htdocs \ blog \ system \ functions.php中假定为'DB_HOST'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!