SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP PDO connection problem

    I'm learning PHP PDO and at the moment I can connect to my database using the traditional mysql_connect() function but not the using the PHP PDO way.

    Here here goes the code using the mysql_connect() function.

    HTML Code:
    <?php
    
    echo 'This is a test';
    
    /*
    database name onetwo_test1
    database onetwo_test1 username onetwo_test1u1 with password dbpw123
    */
    
    $host = 'localhost';
    $username = 'onetwo_test1u1';
    $password = 'dbpw123';
    $dbname = 'onetwo_test1';
    
    $conn = mysql_connect($host, $username, $password) or die('Could not connect: ' . mysql_error());
    
    ?>
    Here goes the code using the PHP PDO way, and yes I know that I should use a try and catch.

    HTML Code:
    <?php
    
    echo 'This is a test';
    
    /*
    database name onetwo_test1
    database onetwo_test1 username onetwo_test1u1 with password dbpw123
    */
    
    $host = 'localhost';
    $username = 'onetwo_test1u1';
    $password = 'dbpw123';
    $dbname = 'onetwo_test1';
    
    $conn = new PDO('mysql:host=localhost;dbname=$dbname', $username, $password);
    ?>
    Thanks

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,395
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    You need to double-quote your connection string, otherwise PHP won't replace $dbname with the actual value:
    PHP Code:
    $conn = new PDO("mysql:host=localhost;dbname=$dbname"$username$password); 

  3. #3
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That didn't work either.

    Here goes the code again and the error code that is being shown on the page.

    Code:
    <?php
    
    echo "This is yet another test <br />";
    
    $host = 'localhost';
    $username = 'onetwo_test1u1';
    $password = 'dbpw123';
    $dbname = 'onetwo_test1';
    
    $conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
    ?>
    Code:
    This is yet another test 
    
    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] [1044] Access denied for user 'onetwo_test1u1'@'localhost' to database 'onetwo_test1'' in /home/onetwo/public_html/samplesite.com/test/test/test5.php:23 Stack trace: #0 /home/onetwo/public_html/samplesite.com/test/test/test5.php(23): PDO->__construct('mysql:host=loca...', 'onetwo_test1u1', 'dbpw123') #1 {main} thrown in /home/onetwo/public_html/samplesite.com/test/test/test5.php on line 23

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    That error means the specified user does not have access to the database.

    Running the below within MySQL from the command prompt should fix the problem.

    Code SQL:
    GRANT ALL ON onetwo_test1.* TO 'onetwo_test1u1'@'localhost';

    Though before you run that double check that the username, database name and password are correct in the script. If those things are all correct than the *likely cause is that the user does not have access to the database itself.
    The only code I hate more than my own is everyone else's.

  5. #5
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. It worked. When I created a new database and user for testing purposes, I completely forgot to assign the user to the database. I went to my cpanel to fix this.

    Thanks a lot!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •