1

我正在构建一个系统,主要用于巩固学习,但将在实践中使用。

我将尝试口头解释我关注的 ER 图的部分:

每个学员可以有许多uniformID

每个 Uniform ID 都是 table uniform 中的一个新条目,因此cadets(table) 可能如下所示:

id | name    | ... | uniformID

1  | Example | ... | 1,2,3

uniform桌子:

id | notes             | cadet

1  | Need new blahh    | 1

2  | Some stuff needed | 1

3  | Whatever you like | 1

再三考虑,看起来我不需要数据库中的第三列。

我正在尝试遍历uniformID中的每个ID,代码:

<?php
$cadet = $_GET['id']; // set  from URL

$query = mysql_query("SELECT `uniformID` FROM `cadets` 
  WHERE id = '$cadet' LIMIT 1") 
  or die(mysql_error()); // get uniform needed as string

// store it
while ($row = mysql_fetch_array($query)) {
  $uniformArray = $row['uniformID']; 
}
echo $uniformArray . "  ";
$exploded = explode(",", $uniformArray); // convert into an array

// for each key in the array perform a new query
foreach ($exploded as $key => $value) {
  $query(count($exploded));
  $query[$key] = mysql_query("SELECT * FROM `uniform` WHERE `id` = '$value'");
}

?>

正如我所说,这主要是出于合并目的,但我遇到了一个错误,sql 说:

致命错误:函数名称必须是第 82 行 C:\wamp\www\intranet\uniform.php 中的字符串

第 82 行是:

$query[$key] = mysql_query("SELECT * FROM `uniform` WHERE `id` = '$value'");

我不确定它会起作用,所以我尝试了它,现在我被卡住了!

编辑:

感谢所有为此做出贡献的人!这是现在的工作代码:

foreach ($exploded as $key => $value) {
              //$query(count($exploded));
              $query = mysql_query("SELECT * FROM `uniform` WHERE `id` = '$value'");
              while ($row = mysql_fetch_array($query)) {
                echo "<tr>
                  <td>" . $row['id'] . "</td>
                  <td>" . $row['note'] . "</td>
                </tr>";
              }
            }

添加了 while 并通过将其嵌套在 foreach 中进行了迭代

4

1 回答 1

1

除了你的桌子

cadets(id, ...)
uniforms(id, ...)

cadets使用描述实体和实体之间关系的叉积表uniforms

cadets_x_uniforms(cadet_id, uniform_id)

对于每个关系(在这种情况下,学员 x 具有统一的 y)将具有相应 id 的记录放入叉积表中。

...嗯,我想在这种情况下,一个工作示例会做得更好...

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);

// fetch the uniforms of a specific cadet
$stmt = $pdo->prepare('
    SELECT
        c.name,u.id,u.labelid
    FROM
        so_cadets as c
    LEFT JOIN
        so_cadet_uniform as cxu
    ON
        c.id=cxu.cadet_id
    LEFT JOIN
        so_uniforms as u
    ON
        cxu.uniform_id=u.id
    WHERE
        c.name=?
');
$stmt->execute( array('cadetB') );
$stmt->setFetchMode(PDO::FETCH_ASSOC);
echo "uniforms of cadetB\n";
foreach( $stmt as $row ){
    echo join(', ', $row), "\n";
}

// fetch cadets without uniforms
$query = '
    SELECT
        c.name
    FROM
        so_cadets as c
    WHERE
        NOT EXISTS(SELECT uniform_id FROM so_cadet_uniform as cxu WHERE c.id=cxu.cadet_id)
';
echo "cadets without uniforms\n";
foreach( $pdo->query($query, PDO::FETCH_ASSOC) as $row ){
    echo join(', ', $row), "\n";
}   


function setup($pdo) {
    $pdo->exec('
        CREATE TEMPORARY TABLE so_cadets (
            id int auto_increment,
            name varchar(32),
            primary key(id)
        )
    ');

    $pdo->exec('
        CREATE TEMPORARY TABLE so_uniforms (
            id int auto_increment,
            labelid varchar(32),
            primary key(id),
            unique key(labelid)
        )
    ');

    $pdo->exec('
        CREATE TEMPORARY TABLE so_cadet_uniform (
            cadet_id int,
            uniform_id int,
            primary key(cadet_id,uniform_id)
        )
    ');


    $stmt = $pdo->prepare('INSERT INTO so_cadets (name) VALUES (?)');
    foreach( range('A','F') as $c ) {
        $stmt->execute( array('cadet'.$c));
    }

    $stmt = $pdo->prepare('INSERT INTO so_uniforms (labelid) VALUES (?)');
    foreach( range('K','Z') as $c ) {
        $stmt->execute( array('label'.$c));
    }

    $stmt = $pdo->prepare('INSERT INTO so_cadet_uniform (cadet_id,uniform_id) VALUES (?,?)');
    $cadetHasUniforms = array(
        1=>array(1,2,3), // <- cadetA
        2=>array(7,9), // <- cadetB
        3=>array(8,5,4), // <- cadetC
        // 4=>array() <- cadetD, no entry, no uniforms
        5=>array(10,13,15) // <- cadetE
        // <- cadetE, no entry, no uniforms
    );
    foreach( $cadetHasUniforms as $cadetId=>$uniformIds ) {
        foreach( $uniformIds as $uid ) {
            $stmt->execute(array($cadetId, $uid));
        }
    }

}

印刷

uniforms of cadetB
cadetB, 7, labelQ
cadetB, 9, labelS
cadets without uniforms
cadetD
cadetF
于 2012-11-08T10:58:45.923 回答