Error connecting to MYSQL

Here is the config.php file

<?php error_reporting(E_ALL ^ E_NOTICE); /*=========== Database Configuraiton ==========*/ $db_host = "localhost"; $db_user = "test"; $db_pass = "test"; $db_name = "dbtest"; /*=========== Website Configuration ==========*/ $defaultTitle = 'testing'; $defaultFooter = date('Y').' &copy; testing'; ?> 

Here is the link to config.php

 <?php require_once "includes/config.php"; require_once "includes/connect.php"; require_once "includes/helpers.php"; header('Cache-Control: max-age=3600, public'); header('Pragma: cache'); header("Last-Modified: ".gmdate("D, d MYH:i:s",time())." GMT"); header("Expires: ".gmdate("D, d MYH:i:s",time()+3600)." GMT"); ?> 

Connect.php below

 <?php /* The login details are taken from config.php. */ try { $db = new PDO( "mysql:host=$db_host;dbname=$db_name;charset=UTF-8", $db_user, $db_pass ); $db->query("SET NAMES 'utf8'"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { error_log($e->getMessage()); die("A database error was encountered"); } ?> 

Does anyone see a problem with this code? I get an error from connect.php “a database error occurred” I need another set of eyes because all my information looks correct and I don’t see an error in the code. Thanks.

+6
source share
4 answers

Try your credentials as soon as possible, instead of using them as a variable.

also try debugging using below:

 catch(PDOException $e) { error_log($e->getMessage()); die("A database error was encountered -> " . $e->getMessage() ); } 

Let me know if it works.

+1
source

you just change utf-8 to utf8 ..

 try { $db = new PDO( "mysql:host=$db_host;dbname=$db_name;charset=utf8", $db_user, $db_pass ); 
+24
source

Did it work before? I think your problem is that "localhost" should be "127.0.0.1" or similar.

0
source

$ db-> query (...) for SELECT, and you set the parameter.

Try

$ db-> exec (...) and this may solve.

0
source

Source: https://habr.com/ru/post/920441/


All Articles