0

I am having an issue with the object creation with PDO.

Below is my code and it gets hangs at "new PDO". and there is no response and no error.

<?php
$user = "root";
$password = "password";
$database = "test";
$table = "test";

try {
  $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
  var_dump($db);
  echo "<h2>TODO</h2><ol>";
  foreach($db->query("SELECT content FROM $table") as $row) {
    echo "<li>" . $row['content'] . "</li>";
  }
  echo "</ol>";
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

Can someone help me understand why I am getting this issue.

I am running ubuntu 20.04

enter image description here

Syed Abdul Qadeer
  • 455
  • 1
  • 6
  • 14
  • Make sure you have error reporting enabled ... To get errors out of PHP even in a LIVE environment add these 4 lines to the top of any MYSQLI_ based script you want to debug ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);. This will force any MYSQLI_ errors to generate an Exception that you can see on the browser as well as normal PHP errors. – Dave Jan 29 '22 at 16:40
  • [**What does your error log tell you?**](https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php-5-apache-fastcgi-and-cpanel) – Martin Jan 29 '22 at 16:40
  • @Martin, there is no error, as the script hangs. i have attached the screenshot – Syed Abdul Qadeer Jan 29 '22 at 17:35
  • When it hangs it could be a dns resolve problem or a connect problem. Can you connect to DB with `telnet localhost 3306` or `telnet 127.0.0.1 3306`? – daniel Jan 29 '22 at 17:57

1 Answers1

0

In PHP Version blow 8 the default PDO-Errormode is ERRMODE_SILENT. Try to set Errmode to ERRMODE_EXCEPTION.

$db = new PDO("mysql:host=localhost;dbname=$database", $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
daniel
  • 273
  • 2
  • 7