不止一次使用MySQL结果数组

时间:2013-08-21 19:35:13

标签: php mysql

我的页面中有以下代码

    <?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个相同下拉菜单的相同值。

我可以将值存储到数组中并在每个下拉列表中重复使用吗?

3 个答案:

答案 0 :(得分:3)

注意:不推荐使用mysql_*函数系列!

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


另一个注意事项:使用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>';

<强>文档

答案 1 :(得分:2)

使用PDO,您可以轻松地将结果提取到数组中,此外还有mysql_* functions are deprecated

<?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>

答案 2 :(得分: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>

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