Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    244
    Rep Power
    12
    I had this originally: I removed the try and catch for this post

    $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
    $database = new PDO('mysql:host=localhost;dbname=YOURDB;charset=utf8', 'YOURUSER', 'YOURPW', $db_options);
    $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

    You saying change to this

    $db_options = array(
    PDO::ATTR_EMULATE_PREPARES => false // important! use actual prepared statements (default: emulate prepared statements)
    , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // throw exceptions on errors (default: stay silent)
    , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC // fetch associative arrays (default: mixed arrays)
    );
    $database = new PDO('mysql:host=localhost;dbname=YOURDB;charset=utf8', 'YOURUSER', 'YOURPW', $db_options);
    // important! specify the character encoding in the DSN string, don't use SET NAMES
  2. #17
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by jaykappy
    You saying change to this
    Yep. What's important is this:
    • Don't use SET NAMES. Instead, specify "charset=..." in the DSN string.
    • Turn off emulated prepared statements, because those can be subject to encoding issues.


    You said you have a "try catch" block surrounding the PDO instantiation. Why? Because it's not a good idea to display something like "database connection failed" or even the exact error message on the page. So a "try catch" doesn't make sense in this context -- unless you want to retry the connection or something.
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    244
    Rep Power
    12
    Im going from this example:
    http://forums.devshed.com/php-faqs-and-stickies-167/how-to-program-a-basic-but-secure-login-system-using-891201.html

    Still learning here...I tried to replace with your code and now getting error

    This was my original code and it works..
    Code:
    <?php 
    
        // These variables define the connection information for your MySQL database 
    	$username = "UserName"; 
        $password = "P@ssw0rd"; 
        $host = "127.0.0.1:2200"; 
        $dbname = "HTMLTest";
    
        // MYSQL
        $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 
        $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
    
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
        $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 
    
        if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
        { 
            function undo_magic_quotes_gpc(&$array) 
            { 
                foreach($array as &$value) 
                { 
                    if(is_array($value)) 
                    { 
                        undo_magic_quotes_gpc($value); 
                    } 
                    else 
                    { 
                        $value = stripslashes($value); 
                    } 
                } 
            } 
         
            undo_magic_quotes_gpc($_POST); 
            undo_magic_quotes_gpc($_GET); 
            undo_magic_quotes_gpc($_COOKIE); 
        } 
        header('Content-Type: text/html; charset=utf-8'); 
        session_start();
    Tried to replace with your suggestion and it DOES NOT WORK
    Code:
    <?php 
    
        // These variables define the connection information for your MySQL database 
    	$username = "username"; 
        $password = "P@ssw0rd"; 
        $host = "127.0.0.1:2200"; 
        $dbname = "HTMLTest";
    
    	$db_options = array( 
    		PDO::ATTR_EMULATE_PREPARES => false                      
    		, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION           
    		, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC      
    		); 
    		
    	// important! specify the character encoding in the DSN string, don't use SET NAMES
    	$db = new PDO('mysql:host={$host};dbname={$dbname};charset=utf8', $username, $password, $db_options);    
    
        if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
        { 
            function undo_magic_quotes_gpc(&$array) 
            { 
                foreach($array as &$value) 
                { 
                    if(is_array($value)) 
                    { 
                        undo_magic_quotes_gpc($value); 
                    } 
                    else 
                    { 
                        $value = stripslashes($value); 
                    } 
                } 
            } 
         
            undo_magic_quotes_gpc($_POST); 
            undo_magic_quotes_gpc($_GET); 
            undo_magic_quotes_gpc($_COOKIE); 
        } 
    
        header('Content-Type: text/html; charset=utf-8'); 
        session_start();
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    244
    Rep Power
    12
    any idea Jacques1 why this is not working....? see my last post
    Thanks for all your help with this..very appreciated...know I have a lot to learn...but gotta start somewhere.
  8. #20
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    244
    Rep Power
    12
    Got it....freakin " "

    Was
    ('mysql:host={$host};dbname={$dbname};charset=utf8',

    Should be
    ("mysql:host={$host};dbname={$dbname};charset=utf8",
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo