Jump to content

PatrickG19872019

New Members
  • Posts

    2
  • Joined

  • Last visited

Posts posted by PatrickG19872019

  1. 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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.