Hi guys,
I'm trying to connect my new project with PDO, and for simple reason witch i don't know and i've no idea i'm getting

connection failedSQLSTATE[28000] [1045] Access denied for user 'www-data'@'localhost' (using password: NO)

I tried everything was in my mind, i tried log to mySql by terminal, i can log properly,and with some credencial work for another connection, native mysql, and i checked if PDO driver is enabled, and is connected, so bellow my pdo settins are:

config.php
<?php
define('DB_NAME', 'dbname');
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'pass');
?>
class.database.php
<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include_once 'connection.php';

class dbConnection{
    protected $db_conn ;
    public  $db_name = DB_NAME;
    public  $db_host = DB_HOST;
    public  $db_user = DB_USER;
    public  $db_pass = DB_PASS   ;

    public function connect(){
        try {

        $this ->db_conn = new PDO("mysql:host = $this->db_host; dbname= $this->db_name", $this->user, $this->pass);
        return $this->db_conn;
        } catch (PDOException $e) {
            echo 'connection failed'. $e->getMessage();
        }
    }

}
?>
class.address.php
<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class manageAddress{
    public $link;

    function __construct() {
        include_once ('class.database.php');
        $conn = new dbConnection();
        $this->link =$conn->connect();
        return $this->link;
    }
    function getData( $table_name, $id = null){
        if(isset($id)){
        $query = $this->link->query("SELECT * FROM $table_name WHERE id = '$id' ORDER BY id ASC");    
        }
        else
        {
        $query = $this->link->query("SELECT * FROM $table_name ORDER BY id ASC");     
        }
        $rowCount = $query ->rowCount();
        if($rowCount >= 1)
        {
            $result = $query->fetchAll();
        }
        else 
        {
            $result = 0; 
    }
    return $result;
    }
}
?>
getData.php
<?php
session_start();

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include_once ('class.address.php');
$init = new manageAddress();

$table_name = 'tbl_users';
$data = $init->getData($table_name);
print_r($data);

?>

So i post all code i'm using for now, please can someone point to right side!

Dani AI

Generated

— good that you fixed the credentials. The blank page / HTTP 500 you see now is almost always a suppressed PHP fatal error (syntax, missing include, or calling a method on false), not a new MySQL auth problem. As hinted, DSN formatting can matter; also make sure your include paths and filenames match exactly (Linux is case sensitive) and that connection.php actually loads.

Enable error output and switch PDO into exception mode so you see the real problem while debugging (turn this off in production):

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$opts = [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  PDO::ATTR_EMULATE_PREPARES => false,
];

$dsn = "mysql:host=$host;dbname=$db;charset=utf8mb4";
$pdo = new PDO($dsn, $user, $pass, $opts);

If the page still fails, check whether your query returned false before calling methods like rowCount() (calling a method on false causes a fatal error). Prefer prepared statements and fetchAll() then count() for SELECTs, or explicitly test the query result and log $pdo->errorInfo(). Also inspect server logs for the exact error:

tail -n 80 /var/log/apache2/error.log
tail -n 80 /var/log/httpd/error_log
journalctl -u php-fpm -e

Common culprits: wrong include paths, file permission issues, a syntax error in an included file, or using rowCount() on a failed query. After enabling errors you will see the exact line that needs fixing.

Recommended Answers

All 2 Replies

Connection string should be like

"mysql:host = $this->db_host; dbname= $this->db_name; $this->user; $this->pass;");

Hi Shark, Thanks for replyng, and u helped solve the error.
I review my code and i found that user and pass was incorrect defined.
so i solved using this line

  $this ->db_conn = new PDO("mysql:host = $this->db_host; dbname = $this->db_name", $this->db_user, $this->db_pass);

So now, my big problem is when i run the file the page is comming blank, no error message, no response for query i made to get data, and i tried debug the browser is getting error 500 Internal server error, so once againg i've no idea how to salve it, i don't know what's missing on php or mysql or apache

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.