Jump to content

Conecting to DB using PDO


deadendstreet

Recommended Posts

So I figured out the form issues I was having (basically, I'm allowing users to add/edit information to a database). It's been working great. Then I did some reading on PDO's. The first step I did was update my db-connect.php code.Now I get a Access denied for user 'user'@'localhost' (using password: NO) error message. Thinking I typed something wrong, I literally copied and pasted the database name, etc. from the earlier version of the file., but it still doesn't work. What would be causing this? I literally followed step-by-step directions from a website I found and in the comments section everyone said it was working.


<?php

$host = 'localhost';
$dbname = 'name';
$user = 'user';
$pass = 'pass';
try
{
    $DB = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
}
catch(PDOException $e)
{  
    echo $e->getMessage();  
}


?>
Link to comment
Share on other sites

is that all the code or do you have other code later in the file?

 

that error is typical of a mysql_query() or other mysql_ function running when there isn't a mysql_connect() database connection and it (the mysql_query() statement) tires to make a connection using default credentials, which are rarely set on any server.

Link to comment
Share on other sites

Nope, that's the code. The tutorial I found showed that to be the only code (in that file).

 

Let me ask this, do you know of a good PDO tutorial that you'd recommend? The ones I've found have been somewhat confusing...granted, I've been under the weather for the past week. Thanks for your help.

Link to comment
Share on other sites

the PDO documentation at php.net is the best starting place.

 

there's nothing technically wrong with the posted code and works for me.

 

the error message mentions NO password, that indicates in your actual code that $pass was empty.

 

the error message in general either means that the database name doesn't exist, with that exact spelling/capitalization, or the user/password combination doesn't exist or hasn't been given permission for the database name being used.

 

if you copied any portion of that from some web site, you may have non-printing characters as part of it that is messing with the values. posting it here could have removed those characters, so copy/pasting it from this forum post may result in working code while your original code won't work.

 

do you have php's error reporting set full on (error_reporting set to E_ALL and display_errors set to ON) as that may help with things like non-printing characters/character set issues in the $pass variable?

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.