5

I´m trying to connect to my database with PDO and show some blogposts on a page. However I´m getting this error message:

Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name' in index.php on line 61...

I´ve been searching for help but really can´t figure out what is wrong so if anyone have any idea it is much appreciated!

I have a separate connect.inc.php file which is included in the index.php file.

This is the connect.inc.php file:

<?php
class DB extends PDO
{
function database_connection() {
   $db_host = "localhost";
   $db_name = "blogdata";
   $db_user = "username";
   $db_pass = "password";
   try {
   global $db_host, $db_name, $db_user, $db_pass;
   $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
   }
   catch(PDOException $e) {
   die( 'Query failed: ' . $e->getMessage() );
}
}
}
?>

And this is the section in the index.php file which is pointed out in the error message:

<?php
    require 'connect.inc.php';  
    $db = new DB('blogdata');

    $query = "SELECT * FROM blogposts";
    if ($result = $db->query($query)) {
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        echo ' 
            <section id="content">
            <article class="post_title"><h3> ', $row['title'],' </h3></article>
            <article class="post_message"> ', nl2br ($row['message']),' </article>
            <article class="post_time"> ',$row['time'],' </article>
            </section>
            ';
        }
    } ;
    ?>
Lisa
  • 416
  • 6
  • 16
  • 29
  • 2
    First of all, this code is extremely inconsistent. Class DB has `database_connection()` method which is called nowhere. You are calling it with 'blogdata' parameter, but again nowhere it's used. Is it *really* the actual code you run? – Your Common Sense Nov 02 '13 at 10:15

3 Answers3

11

Gotcha.

For some reason you are extending your class from PDO. So, your 'blogdata' is taken as a DSN.

Just get rid of your DB class and use raw PDO

connect.inc.php:

<?php 
$db_host = "localhost";
$db_name = "blogdata";
$db_user = "username";
$db_pass = "password";
$db = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

index.php:

<?php
require 'connect.inc.php'; 

$query = "SELECT * FROM blogposts";
$result = $db->query($query);
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Thank you! Finally! That solved the problem! However I see what you mean with your above comment. I´ve been fizzling with my connection code for some time and I had a better consistent code first which I messed up when not knowing exactly what I´m doing:/ Is it better to wrap the connection up in a function than using raw PDO? – Lisa Nov 02 '13 at 10:31
  • Only if you know what are you doing. As a matter of fact, PDO *is* already such a connection handling class. It has almost everything an average user needs. So, it's better to keep with raw PDO. – Your Common Sense Nov 02 '13 at 10:38
  • @YourCommonSense, I did as suggested. But, It's not getting things from connect.inc.php file. As for example: I have created 2function at index.php inside a class. One function is for insertion, another one is for selection. Insertion works fine. But, selection is not able to fetch the connection. So you have any suggestion regarding this? – tanjiya Oct 03 '21 at 06:40
0

Why are you declaring your DB variables global after initialising them? I can't test it but if memory serves that'll pull in existing values from global, overwriting the ones you've just declared.

I disagree with not using an inherited class though - being able to write a custom constructor means you can set all modes and attributes once and then just call the custom constructor to have it done automatically. Much cleaner than having to do it every time.

gedq
  • 609
  • 9
  • 20
0

For future references, you could keep the DB class and refer to the following solution that hasn't much to do with the php class:

enter link description here

Chihab
  • 403
  • 5
  • 11