-1

Connection failed: SQLSTATE[28000] [1045] Access denied for user 'rkcat'@'localhost' (using password: YES) Fatal error: Call to a member function query() on a non-object in

 <?php 
    $servername = "localhost";
    $username = "rkcat";
    $password = "rkcat123!@#";
    enter code here
    try {
      $conn = new PDO("mysql:host=$servername;dbname=shradha_padma", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

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

    $sql = "SELECT * FROM slider";
    $result = $conn->query($sql);
    $data = $result->fetchAll(PDO::FETCH_ASSOC);

     ?>
  • 4
    `Access denied for user 'rkcat'@'localhost'` – Jonnix Jul 05 '17 at 14:42
  • fix your mysql installation, **[look here](https://stackoverflow.com/questions/10299148/mysql-error-1045-28000-access-denied-for-user-billlocalhost-using-passw)** – YvesLeBorg Jul 05 '17 at 14:44

2 Answers2

1

You are getting the Fatal error: Call to a member function query() on a non-object in error due to the SQLSTATE[28000] [1045] Access denied for user 'rkcat'@'localhost' error.

Because the connection failed in your try/catch, the PDO object ($conn) was not initialized, and thus the query() method does not exist when you call it via $conn->query($sql).

To solve the problem, find out why access is denied to your user. (Bad password, or incorrect permissions on your DB)

RToyo
  • 2,792
  • 1
  • 12
  • 21
0

DB Connection failed ,correct your connection params, or check if user "rkcat" has access rights in DB

also exit when you get connection error.

Ahmed Sunny
  • 1,897
  • 1
  • 19
  • 20
  • Why do you recommend that OP exit after getting a connection error? – RToyo Jul 05 '17 at 14:53
  • exit() or return 0 whatever, so the code does not go ahead if connection failed, like in this case, he did not saw the connection error, and gets the second error which he thought was the main error , instead of connection error, which was obvious at first place, no connection no query . – Ahmed Sunny Jul 05 '17 at 15:03
  • Perhaps I'm misunderstanding what you intended with "exit". The OP should try to gracefully output the error and close out his script; not abruptly `exit()` or `die()`. I have a feeling that's what you were meaning as well. – RToyo Jul 05 '17 at 15:05
  • 'echo "Connection failed: " . $e->getMessage(); exit();' he will get connection error only, and not the second error, because in his case, the problem was with connection – Ahmed Sunny Jul 05 '17 at 15:07
  • I'm moving a bit off topic, and bringing up a rather petty issue, but doing a hard exit() won't be accepted too well in a production setting. Most websites have footers, menus, etc, that should be displayed, even when the "content" of the page is simply an error message. It's better to allow your code to fail gracefully, without affecting the rest of the output to the browser. I agree that a query shouldn't be attempted if $conn is not initialized; but a hard exit should usually be a last resort. – RToyo Jul 05 '17 at 15:21
  • You are right,i understand what you mean,mostly,we use frameworks, so everything is handled properly, but for this patch of code which he shared, i said to add exit,because he is getting two errors, and the second error is just because of 1st one. but if you are using a core php code, i will first test the connection and then run anything else, & in production , you should not display these errors,or if you have too just create an error page and print error there, like no connection or 404,403,etc, so you have your header, etc – Ahmed Sunny Jul 05 '17 at 15:38