[英]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.您用于Host
、 User
和Password
的常量尚未定义。 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.