-2

I'm coding a website in php but when I tested it, the page indicates HTTP ERROR 500. So I searched for the problem and I found a log file which indicated me the following content:

'ERR_WARNING' in /Applications/MAMP/htdocs/SITE/config/connect.php:3
Stack trace:
#0 /Applications/MAMP/htdocs/SITE/config/functions.php(5): require()
#1 /Applications/MAMP/htdocs/SITE/index.php(4): getArticles()
#2 {main}
   thrown in /Applications/MAMP/htdocs/SITE/config/connect.php on line 3

Then I checked the file functions.php but found no errors could you please help me fix this problem.

Here is my functions.php file:

<?php
// ARTICLE FUNCTION
    function getArticles()
    {
        require "./config/connect.php";
        $req = $bdd->prepare('SELECT id, title FROM articles ORDER BY id DESC');
        $req->execute();
        $data = $req->fetchAll(PDO::FETCH_OBJ);
        return $data;
        $req->closeCursor();
    }
?>

Here is my connect.php file:

<?php 
    $bdd = new PDO('mysql:host=localhost;dbname=blog;charset=utf8','root','root'); 
    $bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERR_WARNING); 
?>

Then, I placed the replace command in function.php under the close cursor one, however, it didn't solve the problem

Then, I changed the password in my connect file and in my database so that they are now the same one.

But it didn't solve the problem showing me the following message:

[09-Apr-2019 15:15:31 UTC] PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: >YES) in /Applications/MAMP/htdocs/SITE/config/connect.php:2
Stack trace:
#0 /Applications/MAMP/htdocs/SITE/config/connect.php(2): PDO->__construct('mysql:host=loca...', 'root', '123')
#1 /Applications/MAMP/htdocs/SITE/config/functions.php(5): require('/Applications/M...')
#2 /Applications/MAMP/htdocs/SITE/index.php(4): getArticles() #3 {main}
thrown in /Applications/MAMP/htdocs/SITE/config/connect.php on line 2

Then, I realised that the password I put in the connect file was wrong and I changed it. It took me back to the first error:

[09-Apr-2019 16:40:18 UTC] PHP Fatal error: Uncaught Error: Undefined class constant 'ERR_WARNING' in /Applications/MAMP/htdocs/SITE/config/connect.php:3
Stack trace:
#0 /Applications/MAMP/htdocs/SITE/config/functions.php(5): require()
#1 /Applications/MAMP/htdocs/SITE/index.php(4): getArticles()
#2 {main}
thrown in /Applications/MAMP/htdocs/SITE/config/connect.php on line 3

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Hatsingue
  • 29
  • 1
  • 5
  • As I'm using a mac device I don't think this method will work – Hatsingue Apr 09 '19 at 14:46
  • Glad you solved your problem, but there is no need to mark questions as resolved. See [why we remove “solved” from question titles](https://meta.stackoverflow.com/q/311829). – Martijn Pieters May 17 '19 at 15:02

1 Answers1

1

I finally found the solution. In the connect file my line 3 was:

$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERR_WARNING);

However there was a typo: constant ERR_WARNING doesn't exist – it should be: ERRMODE_WARNING. Replacing that constant with the correct name fixed the problem:

$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

Thank you for all your answers and excuse to have bothered your time for this simple problem

divibisan
  • 11,659
  • 11
  • 40
  • 58
Hatsingue
  • 29
  • 1
  • 5