First, take a look at my code

<?php

try
{
    $dbh = new PDO('mysql:host=$db_host;dbname=blogproject1','root','');
    $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $dbh->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
    die('Error: ' . $e->getMessage());
}

if(!defined('GUEST')) {
    define('BASEPATH', dirname(dirname(__FILE__));
    header('Location: ' . BASEPATH);
    exit();
}

When it executed it gives me an error.. Looks like, the error is at the header function... Can you tell me how to fix it?

What is the exact error?

Most likely, the connection failed, echoed an error, causing the header not to function. I think it should be:

SET NAMES 'utf8'

Single quotes, not double.

You can use it in connection string like

$dbh = new PDO('mysql:host=$db_host;dbname=blogproject1;charset=utf8','root','');

instead of

$dbh->exec('SET NAMES "utf8"');

OR else you can use the same but you are having a syntax error

$dbh->exec('SET NAMES "utf8"');

should be

$dbh->exec("set names utf8");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.