PDO invalid data source name

I’m new to using PDO, I’m trying to get the php code below to work, but whatever I do I get this error:

PDO Exception Caught. Error with the database:
SQL Query: Error: invalid data source name

I’m running wampserver latest version on windows xp pro sp3, all the pdo extensions are enabled. I’ve tried quote, but it still gives the same error.
My dsn is correct:

$dsn = 'mysql:host=localhost;dbname=dbname;';
$user = 'user';
$password = 'password';
   function loginnow()     {
	 $username = trim($_POST['username']);
	 $password = trim($_POST['password']);
	
$crypto = new phpFreaksCrypto();
$password = $crypto->encrypt($password);
echo $username. '<br />' ;
echo $password . '<br />';

          session_start();
		 try
		{
		 $dbh = new PDO($dsn, $user, $password);
		 $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$stmt = $dbh->prepare ('SELECT * FROM admin WHERE username= :username and password= :password');
		$stmt->bindValue(':username', $username);
		$stmt->bindValue(':password', $password);
		$stmt->execute();
		$admin = $stmt->fetchAll();

			
/*			$sql = "SELECT * FROM admin WHERE username=:username and password=:password";
			
			$stmt = $dbh->prepare($sql);
			
			$stmt->bindParam(':username', $username);
			
			$stmt->bindParam(':password', $password);
			
			$stmt->execute(); */
			}
		 catch (PDOException $e)
			{

			 echo 'PDO Exception Caught.  ';

			 echo 'Error with the database: <br />';

			 echo 'SQL Query: ', $sql;
	
			 echo 'Error: ' . $e->getMessage();
	
			}

 /*	  echo "<pre>user is\
".var_dump($admin)."</pre>";
         $_SESSION['admin'] = $admin;
          if ($admin !== false)     {
               redirect("menu.php");
          } else {
               redirect("login.php");
          }*/
     }

$dbh = new PDO($dsn, $user, $password);

Shouldn’t that be:

   $dbh = new PDO($dsn, $username, $password); 

Its not clear whether pdo is even connected.

Why don’t you try a simpler select, like a test db with jut one field an a few rows in it?

I got this code below to work, but no luck with the other.

$province = 2;
		 $dbh = new PDO($dsn, $user, $password);
		 $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$sql = 'SELECT churchname FROM churchdetail WHERE provinceID=:province';
		$stmt = $dbh->prepare($sql);
		$stmt->bindParam(':province', $province);
		$stmt->execute();
		$church = $stmt->fetchAll();
		foreach ($church as $row) {
		print $row['churchname'] . "<br>";
		}

I ran the code without catch exception and it looks like to me the username variable isn’t be carried into the sql where the password variable is

Fatal error: Uncaught exception ‘PDOException’ with message ‘invalid data source name’ in F:\wwwroot\ultitecwww\login.php:35 Stack trace: #0 F:\wwwroot\ultitecwww\login.php(35): PDO->__construct(‘’, NULL, ‘password’) #1 F:\wwwroot\ultitecwww\login.php(7): loginnow() #2 {main} thrown in F:\wwwroot\ultitecwww\login.php on line 35

Try giving your PDO connection username variable and your test data username variable different variable names - cause that looks really confusing to me.

You have proved there’s nothing wrong with your pdo connection - it looks like they are sharing the same password to me.

If in doubt try and retain some sanity by putting real values into your sql and work backwards out of it till you find the problem.

I had to make $dsn, $user, $password global for the script to work.