1

我一直在阅读有关 MySQL 外键和使用父/子关系表的信息。

下面是表关系(phpmyadmin)。表“dpuchanges”与表“opendpu”的列 (ECRNUM) 具有外键 (PARENT) 关系。

出于某种原因,我没有提取任何数据(请参阅下面的 php 代码)。我在想它是我的 SQL 语句,但我不知道哪里出错了。我没有收到任何 PHP 错误。任何人都可以帮忙吗?

在此处输入图像描述

在此处输入图像描述

<!DOCTYPE html>
<html>
<head></head>
<body>
  <table>
  <?php
  require ('config.php');

  $db = null;
  $limit = 10;
  $counter = 0;

  while (true) {
  try {
    $db = new PDO($dsn, $uname, $pword);
    $db->exec( "SET CHARACTER SET utf8" );
    $db->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC ); 
    $db->setAttribute( PDO::ATTR_PERSISTENT, true );
    break;
  }
    catch (Exception $e) {
        $db = null;
        $counter++;
        if ($counter == $limit)
            throw $e;
    }
  }
$stmt = $db->prepare("SELECT ACTION, PARTNO, ACTIONTXT, REV_WAS, REV_NOW, QTY_FROM, QTY_TO FROM dpuchanges JOIN opendpu ON opendpu.ECRNUM = dpuchanges.PARENT WHERE opendpu.ECRNUM = 82095");
$stmt->execute();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $action=$row["ACTION"];
            $partno=$row["PARTNO"];
            $actiontxt=$row["ACTIONTXT"];
            $rev_was=$row["REV_WAS"];
            $rev_now=$row["REV_NOW"];
            $qty_from=$row["QTY_FROM"];
            $qty_to= $row["QTY_TO"];
?>
<tr>
  <td>Action = <?php echo $action; ?></td>
  <td>PartNo = <?php echo $partno; ?></td>
  <td>Description = <?php echo $actiontxt; ?></td>
  <td>REV WAS = <?php echo $rev_was; ?></td>
  <td>REV NOW = <?php echo $rev_now; ?></td>
  <td>QTY FROM = <?php echo $qty_from; ?></td>
  <td>QTY TO = <?php echo $qty_to; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
4

2 回答 2

1

您的相关列“dpuchanges.parent”和“ openpdu.ecrnum ”的数据类型和大小必须相同。

来源:http ://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html

于 2013-11-08T15:42:03.383 回答
1

我想你不见了;

  <td>Action = <?php echo $action; ?></td>
  <td>PartNo = <?php echo $partno; ?></td>
  <td>Description = <?php echo $actiontxt; ?></td>
  <td>REV WAS = <?php echo $rev_was; ?></td>
  <td>REV NOW = <?php echo $rev_now; ?></td>
  <td>QTY FROM = <?php echo $qty_from; ?></td>
  <td>QTY TO = <?php echo $qty_t; ?></td>
于 2013-11-08T15:22:58.150 回答