Note: using undefined DB_HOST constant - assumed to be "DB_HOST" in C: \ xampp \ htdocs \ blog \ system \ functions.php on line 31

I get a couple of mistakes and I can’t see for life where I fall. Below is the function 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 decided that I should include the page that I am launching in order to cause an 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 is much appreciated, these are errors in their entirety.

Note: using the undefined DB_HOST constant - it is assumed that DB_HOST in C: \ xampp \ htdocs \ blog \ system \ functions.php on line 31

Note: using the undefined constant DB_USER - it is assumed that DB_USER is in C: \ xampp \ htdocs \ blog \ system \ functions.php on line 31

Note: using the undefined DB_PASS constant - it is assumed that DB_PASS in C: \ xampp \ htdocs \ blog \ system \ functions.php on line 31 is an invalid data source name Note: undefined variable: dbh in C: \ xampp \ htdocs \ blog \ system \ functions.php on line 37

Fatal error: call prepare () member function for object in C: \ xampp \ htdocs \ blog \ system \ functions.php on line 37

Must also include a configuration file

 <?php define('DB_HOST','mysql:host=localhost;dbname=blog'); define('DB_USER','root'); define('DB_PASS',''); ?> 
+5
source share
3 answers

The constants you use for Host , User and Password are not yet defined. There is probably something wrong with your config.php .

+2
source

which means that DB_HOST is undefined or DB_HOST not available in the current context.

Try putting these variables in a script at the beginning rather than include('config.php');

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

Use special terms instead of links ...

For instance,

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

Change to...

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

It will work.

0
source

All Articles