-4

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>
5
  • So, how do I fix it? :D Commented Jan 4, 2015 at 15:26
  • Try: $db = new PDO('mysql:host=localhost;port=3306;dbname=boekenlijst', 'root',''); (or which port you use) Commented Jan 4, 2015 at 15:28
  • 1
    Sounds like your MySQLd is not running. Or running on a different port. Commented Jan 4, 2015 at 15:29
  • MySQL is not running on localhost:3306 (Where 3306 is the default port). Commented Jan 4, 2015 at 15:31
  • 1
    Try replacing the host, from "localhost" to "127.0.0.1" Commented Jan 4, 2015 at 15:31

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, PDO won't throw catchable errors by default, can you add setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); to your answer?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.