0

我正在尝试减少此页面建立的连接数。我读到的所有内容都说只有一个连接就足够了,但是如果我删除了页面没有连接到我的服务器的其他连接并为我提供了我正在寻找的结果。也是我使用两次的查询之一,但如果我第二次调用原始查询,它也不起作用。

我究竟做错了什么?

<?php
$dbaddress="localhost";
$dbuser="testuser";
$dbpass="testpass";
$dbname="testdb";
$dbtable="elo";
$query="SELECT Sequnce, mcacctname FROM  `elo`;";
$con=mysqli_connect($dbaddress,$dbuser,$dbpass,$dbname);
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
mysql_connect($dbaddress, $dbuser, $dbpass);
mysql_select_db($dbname);
$sql = "SELECT Sequence, mcacctname FROM  `elo`;";
$result = mysql_query($sql);
mysql_connect($dbaddress, $dbuser, $dbpass);
mysql_select_db($dbname);
$sql2 = "SELECT Sequence, mcacctname FROM  `elo`;";
$result2 = mysql_query($sql2);
$sqlstart = "SELECT mcacctname, elo  FROM  `elo`;";
$q = mysql_query($sqlstart);
?>
<form name="player1" method="post" action="predictions.php">
<label for="Select Player 1">Select Player 1:
<?php
echo "<select name='elouser1'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['Sequence'] . "'>" . $row['mcacctname'] . "</option>";
}
echo "</select>";
?>
</label>
<input type="submit" value="Player 1 Wins">
</form>
<br>
<form name="player2" method="post" action="predictions.php">
<label for="Select Player 2">Select Player 2:
<?php
echo "<select name='elouser2'>";
while ($row2 = mysql_fetch_array($result2)) {
    echo "<option value='" . $row2['Sequence'] . "'>" . $row2['mcacctname'] . "</option>";
}
echo "</select>";
?>
</label>
<input type="submit" value="Player 2 Wins">
</form>
<table>
<tr>
<?
echo '<div class="container">';
while($res = mysql_fetch_array($q)){
echo '<tr><td><div class="item">'.  $res['mcacctname'] . '</td><td>' . $res    ['elo'] . '</div></td></tr>';
}
echo '</div>';
mysqli_close($con);
?>
</tr>
</table>
4

1 回答 1

0

在页面中打开连接后,您无需为每个查询重新打开它。您可以简单地删除所有创建连接的语句并选择数据库,除了第一个语句。

请注意,您mysqli用于打开连接,但mysql用于查询它。两组功能不可互换:mysqli不推荐使用“mysql”,将来将删除支持。

尝试这个:

$dbaddress="localhost";
$dbuser="testuser";
$dbpass="testpass";
$dbname="testdb";
$dbtable="elo";
$query="SELECT Sequence, mcacctname FROM  `elo`;";
$con=mysqli_connect($dbaddress,$dbuser,$dbpass,$dbname);
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$sql = "SELECT Sequence, mcacctname FROM  `elo`;";
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
$sql2 = "SELECT Sequence, mcacctname FROM  `elo`;";
$result2 = mysqli_query($con,$sql2) or die(mysqli_error($con));
$sqlstart = "SELECT mcacctname, elo  FROM  `elo`;";
$q = mysqli_query($con, $sqlstart) or die(mysqli_error($con));
?>
<form name="player1" method="post" action="predictions.php">
<label for="Select Player 1">Select Player 1:
<?php
echo "<select name='elouser1'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Sequence'] . "'>" . $row['mcacctname'] . "</option>";
}
echo "</select>";
?>
</label>
<input type="submit" value="Player 1 Wins">
</form>
<br>
<form name="player2" method="post" action="predictions.php">
<label for="Select Player 2">Select Player 2:
<?php
echo "<select name='elouser2'>";
while ($row2 = mysqli_fetch_array($result2)) {
    echo "<option value='" . $row2['Sequence'] . "'>" . $row2['mcacctname'] . "</option>";
}
echo "</select>";
?>
</label>
<input type="submit" value="Player 2 Wins">
</form>
<table>
<tr>
<?
echo '<div class="container">';
while($res = mysqli_fetch_array($q)){
echo '<tr><td><div class="item">'.  $res['mcacctname'] . '</td><td>' . $res    ['elo'] . '</div></td></tr>';
}
echo '</div>';
mysqli_close($con);
?>
</tr>
</table>
于 2013-10-24T00:36:32.117 回答