PDO Error: invalid data source name

This is driving me crazy! I know my values are correct. The database name, username, password are all valid, yet I keep getting this cursed message. Can anyone spot what I’m doing wrong?

<?php
$dsn = 'mysql:host=localhost;dbname=my_guitar_shop1';
$username = 'mgs_user';
$password = 'pa55word';


function connect() {
  try {
	  $db = new PDO($dsn, $username, $password);
	  $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  } catch (PDOException $e) {
	  die( 'Query failed: ' . $e->getMessage() );
  }
}

connect();

?>

Ok.

On your dsn construction you perhaps should have first the dbname and only after the host.

See this php.net example:

<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

Note (for future reference) of if the code above didn’t do it:
Try also the variation here: 127.0.0.1 instead of localhost. I had worked on systems that complain about this. :s

Please let me know the result. :slight_smile:

I tried your solution (thanks) but it didn’t help. I still get the same error message.

If you literally copy/paste this last code, by only changing your databasename, username and password, do you get the same error ?

I’ve read some time ago, something weird (because I don’t understand), but you may try do define $dsn $user and $password as global.

global $dsn $user $password

Let me know. :slight_smile:

Declaring the global variables in the function worked! Thanks a million…it was freaking me out big-time!

I’ll tell you why it wasn’t working. It is called variable scope.
PHP: Variable scope - Manual

update: follow logic_earth link. :slight_smile:

<?php
$a = 1; /* global scope */ 

function test()
{ 
    echo $a; /* reference to local scope variable */ 
} 

test();
?>

This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope.

So, it seems that if you declared the variables inside the function as global, you will change their scope from (default) local to global, hence, allowing you to access those values (declared outside the function scope).

This was what you did ?

Yeah, I actually knew that, but forgot! Here’s the code that works:

<?php

$dsn = "mysql:dbname=riverbed";
$username = 'bopjo1';
$password = 'jambo1';


function connect() {
	global $dsn, $username, $password;
  try {
	  $db = new PDO($dsn, $username, $password);
	  $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  } catch (PDOException $e) {
	  die( 'Query failed: ' . $e->getMessage() );
  }
}

?>

BTW, I created this code to test the same problem I was having with a database connection class I wrote. I was getting the same error so I simplified it into a small script that I could test. Here’s the code for that:

<?php
class DataObject
{
	
  private $host = 'mysql:host=localhost;dbname=riverbed';
  private $username = 'xxxxxx';
  private $password = 'xxxxxx';


  public function connect() {
	  try {
		  $conn = new PDO($this->host, $this->username, $this->password);
		  $conn->setAttribute( PDO::ATTR_PERSISTENT, TRUE );
		  $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
	  } catch ( PDOException $e ) {
		  die( 'Connection failed: ' . $e->getMessage() );
	  }
	  return $conn;
  }
	
  public function disconnect( $conn ) {
	  $conn = '';
  }

} // end class
?>

Note the $this->host, $this->username etc. Kind of the same thing. I forgot to use $this-> to reference the variables.

Glad it worked. :slight_smile:

Cheers.