duanqin2026 2015-01-04 15:24
浏览 45

PHP,奇怪的错误[重复]

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>
</div>
  • 写回答

1条回答 默认 最新

  • dqzow3859 2015-01-04 15:31
    关注

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口