-5

So I have got this PHP program, and when I run it it works but when I click my button which references to the next .php file I get this error;

SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. Notice: Undefined variable: db in C:\xampp\htdocs\school\SEDbekijken.php on line 20

Fatal error: Call to a member function query() on null in C:\xampp\htdocs\school\SEDbekijken.php on line 20

What is the problem?

Here are my PHP files:

<html lang="nl">
<head>
<meta charset="utf-8">
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type ="text/css" href="boek.css">
<title>Boekenlijst van leerlingen</title>
</head>
<body>
<IMG SRC="3.png"></a>   
<hr>
Op dit moment is het:
<?php echo date("d-m-Y, G:i");?>
<hr>
<br>
 Wat wilt u doen?
<br>
<br>
  <a href="SEDbekijken.php"><IMG SRC="1.png"></a>
<br>
  <a href="SEDtoevoegen.php"><IMG SRC="2.png"></a>    
</body>
</html>

When I click that ahref SEDbekijken.php i get the error

This is SEDbekijken.php:

<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type ="text/css" href="boek.css">
<title>Boekenlijst</title>
</head>
<body>
<?php
  // Maken van verbinding
  try { 
    $db = new PDO('mysql:host=localhost;dbname=boekenlijst', 'root','');
  }
  catch(PDOException $e) {
    echo $e->getMessage();
  } 
  // De SQL opdracht
  $sql = 'SELECT * FROM boeken';
  $resultaat = $db->query($sql);
  // De HTML-tabel opbouwen
  echo '<table border = 1>';
  foreach($resultaat as $row) {
    echo '<tr>';
    $nummer = $row['BoekNummer'];
    echo '<td>'.$row['BoekNummer'].'</td>';
    echo '<td>'.$row['Auteur'].'</td>';
    echo '<td>'.$row['Titel'].'</td>';
    echo '<td>'.$row['PlaatsNaam'].'</td>';
    echo '<td>'.$row['Jaar'].'</td>';
    echo '<td>'.$row['AantalPunten'].'</td>';
    echo '<td>'.$row['NaamLeerling'].'</td>';
    echo "<td><form action='SEDwijzigen.php' method='post'>
      <input type='hidden' name='verstopt' value=$nummer>
      <input type='submit' name='wijzig' value='wijzig'>
      </form></td>";
    echo "<td><form action='SEDverwijderen.php' method='post'>
      <input type='hidden' name='verstopt' value=$nummer>
      <input type='submit' name='verwijder' value='verwijder'></form></td>";
    echo '</tr>';
  }
  echo '</table>';
  // Sluiten van verbinding
  $db = NULL;
 ?> 
</body>
</html>
CristianoWilson
  • 141
  • 1
  • 19

1 Answers1

0

here's a realted answer to your error: https://stackoverflow.com/a/25616113/2460773

Default MySQL port is 3306, if you use default settings, you dont need to specify the port. if you change it, you need to manually specify port number,

another reason could be an active firewall that needs to be configured.

eiter way, if you have a fatal error uppon creating connection to the DB, it's better to use 'exit'; in the catch block, so that the rest of the code will not get executed:

try { 
  $db = new PDO('mysql:host=localhost;dbname=boekenlijst', 'root','');
  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {

  echo $e->getMessage();
  exit;
}
Community
  • 1
  • 1
Nitsan Baleli
  • 5,393
  • 3
  • 30
  • 52
  • Hi, PDO won't throw catchable errors by default, can you add setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); to your answer? – Richard87 Jan 04 '15 at 15:40