0

我的页面中有以下代码

    <?php
    $dbc = mysql_connect();
    $db = mysql_select_db();
    $results= mysql_query("SELECT * FROM tbl_teams");
?>
<div class="datagrid"><table>
<thead><tr><th>header</th><th>header</th><th>header</th></tr></thead>
<tbody>
<tr>
<td>
<select name="game1_team1"><option value="0">Choose Team 1</OPTION><?php while($row = mysql_fetch_array($results)) {echo '<option value="'.$row['team_ID'].'">'. $row['team_name'].'</option>';}?></select>
</td>
<td>Vs.</td>
<td>
<select name="game1_team2"><option value="0">Choose Team 2</OPTION><?php while($row = mysql_fetch_array($results)) {echo '<option value="'.$row['team_ID'].'">'. $row['team_name'].'</option>';}?></select>
</td>
</tr>

代码在下拉菜单 game1_team1 中显示来自 MySQL 表的足球队名称,但不在 game1_team2 中;好像我不能两次使用相同的查询。我该如何补救?我想对页面上的 60 个相同的下拉菜单使用相同的值。

我可以将这些值存储到一个数组中并在每个下拉列表中重新使用它们吗?

4

3 回答 3

3

注意:mysql_*函数系列已弃用!

这意味着什么?这意味着您今天应该停止使用它。旧代码在时间和预算允许的情况下更新,但您永远不应该编写涉及这些功能的新代码行


SELECT *...另一个注意事项:使用- 明确命名您将在查询中使用的列是不好的做法。这样,如果表结构发生变化,您可以检测到,因为您的查询将失败,并做出相应的反应(在代码中)。这是被称为防御性编码的更广泛概念的一部分。

面对眼前的问题!

您可以使用 PDO 从数据库中获取结果数组。然后,您可以根据需要多次使用该数组!

// simple sample for connecting with PDO
$host="my.host.name"; // Host name
$username="my_user_name"; // username
$password="my password"; // password
$db_name="my_database_name"; // Database name
$pdo = new PDO('mysql:host='.$host.';dbname='.$db_name, $username, $password);

// actually do the query
$statement= $pdo->prepare('
        SELECT 
            time_ID,
            team_name 
        FROM 
            `tbl_teams` 

');
$statement->execute();
// get an array with all the results
$results = $statement->fetchAll(PDO::FETCH_ASSOC);

之后,您可以循环数组以创建一组选项,或对数据执行任何其他操作。例如:

$options = array();
foreach ($results as $one_result){
 $options[] = '<option value="'.$one_result['team_ID'].'">'.$one_result['team_name'].'</option>';
}
print '<select name="someSelectElement">'.implode('', $options).'</select>';

文档

于 2013-08-21T19:45:57.927 回答
2

使用PDO,您可以轻松地将结果提取到数组中,此外,这些mysql_*函数已弃用

<?php
    $db = new PDO( ... );
    $stmt = $db->prepare("SELECT * FROM tbl_teams");
    $stmt->execute();

    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="datagrid">
<table>
<thead><tr><th>header</th><th>header</th><th>header</th></tr></thead>
<tbody>
<tr>
<td>
<select name="game1_team1">
    <option value="0">Choose Team 1</OPTION>
    <?php
    foreach($results as $team) {
        echo "<option value='{$team['team_ID']}'>{$team['team_name']}</option>";
    }
    ?>
</select>
</td>
<td>Vs.</td>
<td>
<select name="game1_team2">
    <option value="0">Choose Team 2</OPTION>
    <?php
    foreach($results as $team) {
        echo "<option value='{$team['team_ID']}'>{$team['team_name']}</option>";
    }
    ?>
</select>
</td>
</tr>
</tbody>
</table>

但是,为了提高效率,我会这样做:

<?php
    $db = new PDO( ... );
    $stmt = $db->prepare("SELECT * FROM tbl_teams");
    $stmt->execute();

    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    $teamList = '';
    foreach($results as $team) {
        $teamList .= "<option value='{$team['team_ID']}'>{$team['team_name']}</option>";
    }
?>

...
<select name="game1_team1">
    <option value="0">Choose Team 1</OPTION>
    <?php echo $teamList; ?>
</select>
...
<select name="game1_team2">
    <option value="0">Choose Team 2</OPTION>
    <?php echo $teamList; ?>
</select>
于 2013-08-21T19:39:55.593 回答
0

你也可以使用MYSQLI_*

<?php
$dbc = mysqli_connect('host','user','password','db_name');
$results= mysqli_query("SELECT `team_ID`,`team_name` FROM tbl_teams");
?>
<div class="datagrid">
<table>
<thead><tr><th>header</th><th>header</th><th>header</th></tr></thead>
<tbody>
<tr>
<td>
<select name="game1_team1">
<option value="">Choose Team 1</OPTION>
<?php while($row1 = mysqli_fetch_array($dbc ,$results)) { echo '<option value="'.$row1['team_ID'].'">'. $row1['team_name'].'</option>';} ?>

</select>
</td>
<td>Vs.</td>
<td>
<select name="game1_team2">
<option value="">Choose Team 2</OPTION>
<?php while($row2 = mysqli_fetch_array($dbc ,$results)) {echo '<option value="'.$row2['team_ID'].'">'. $row2['team_name'].'</option>';}?>
</select>
</td>
</tr> 
</tbody>
</table>

它现在可以工作了.. :)

于 2013-08-21T20:24:37.053 回答