PatrickG19872019
-
Posts
2 -
Joined
-
Last visited
Posts posted by PatrickG19872019
-
-
Hello, I am trying to get this website working but i get an error saying invalid data source name.
I am fairly certain the error isĀ from one of these 2 files.
db_connection.php
<?php function OpenCon() { $dbhost = "localhost"; $dbuser = "root"; $dbpass = "123"; $db = "db"; $conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error); return $conn; } function CloseCon($conn) { $conn -> close(); } ?>
and the other file index.php
/* // <? /* php require('includes/db_connection.php'); */ ?> */ <? php include 'db_connection.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Blog</title> <link rel="stylesheet" href="style/normalize.css"> <link rel="stylesheet" href="style/main.css"> </head> <body> <div id="wrapper"> <h1>Blog</h1> <hr /> <?php $db = null; global $stmt; try { // $stmt = db->query('SELECT postID, postTitle, postDesc, postDate FROM blog_posts ORDER BY postID DESC'); $conn = new PDO('SELECT postID, postTitle, postDesc, postDate FROM blog_posts ORDER BY postID DESC'); while($row = $stmt->fetch()){ echo '<div>'; echo '<h1><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h1>'; echo '<p>Posted on '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>'; echo '<p>'.$row['postDesc'].'</p>'; echo '<p><a href="viewpost.php?id='.$row['postID'].'">Read More</a></p>'; echo '</div>'; } } catch(PDOException $e) { echo $e->getMessage(); } ?> </div> </body> </html>
Please help me with this error, I would really like this to work.
Thank you very much.
Help with my php/database code
in PHP Coding Help
Posted
I'm sorry I don't understand what you mean. Can you please explain this in more detail. This is from a website that shows your how to create a blog from scratch so i don't have any actual experience with php and don't know how to connect properly.