PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 20, 2022

[FIXED] What is the issue in this PHP code to connect on localhost XAMPP

 April 20, 2022     connection, mysql, php     No comments   

Issue

The error is: Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'moomen'@'localhost' (using password: YES)

Code is:

<?php
$servername = "localhost";
$username = "moomen";
$password = "9124279123";
$dbname = "ecocaa";
try {
$conn = new PDO("mysql:host=$servername;dbname=$servername", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";

} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}

Solution

I can see a problem in your code In the dbname you have used server name, change the code it will work.



Answered By - Subhashis Pandey
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing