0

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?

3
Contributors
2
Replies
29
Views
4 Years
Discussion Span
Last Post by code_rum
0

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.

Edited by pritaeas

0

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");
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.