0

Here is my code which I am using to connect my database. My credentials are perfectly working when directly login with MySQL console but the same is not working when trying to connect DB from my PHP code.

Error ;PHP Warning: mysqli_connect(): (HY000/1045): Access denied for user

    /* Attempt MySQL server connection. Assuming you are running MySQL
    server with default setting (user 'root' with no password) */
    $servername = 'localhost';
    $db_name = 'HappaningsDb';
    $username = 'SIDSAHU3';
    $password = 'sid';
    $conn =null;

    //$link = new PDO("mysql:host=$servername;dbname=$db_name", $username, $password);
    //  $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $link = mysqli_connect($servername, $username, $password, $db_name);

   // Check connection
   if($link == false){
   die("ERROR: Could not connect. " . mysqli_connect_error());
   }

   // Attempt insert query execution
   $sql = "insert into Question_data(QUESTION_ENG,OP_ENG, TOPIC_ID, CAT_ID, IS_PREMIUM, IS_HIN, 
   QUESTION_HIN ,OP_HIN, ANSWER, CREATE_USER,CREATE_DT,UPDATE_USER, UPDATE_DT)values('hsdvhbsvsvk', 
   'dhvjd','1','1','N','N','','','2','sid',curdate(),'','');";
   if(mysqli_query($link, $sql)){
   echo "Records added successfully.";
   } else{
   echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
   }

  // Close connection
   mysqli_close($link);

Please find attached screenshot:
Please find attached screenshot

Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

-1

User SIDSAHU3 could be restricted to Local host

From your CLI, try creating user with %

CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypass';
GRANT ALL ON *.* TO 'myuser'@'%';
Ganesh Chandrasekaran
  • 1,578
  • 12
  • 17