我想減少這個頁面連接的數量。我讀過的所有內容都表明,只有一個連接應該足夠,但如果我刪除了其他連接,則該頁面不會連接到我的服務器,併爲我提供我期待的結果。也是我正在使用兩次的查詢之一,但如果我第二次調用原始查詢,它也不起作用。如何減少連接到mysql服務器的php變量和連接的數量?
我在做什麼錯?
<?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>
你可以做的(全部獨家)移動到'MySQLi_'並使用['mysqli_multi_query()'](http://php.net/manual/en/ mysqli.multi-query.php)我只是不確定該函數是否存在MySQL_' - 我注意到你正在使用'MySQL_'和'MySQLi_'刪除'MySQL_'並使用/傳遞'$ con'來您的查詢。這樣你只能使用一個連接(呼叫)。即:'$ query = $ con> query(「SELECT * FROM ...' –
@ Fred-ii-'mysql _ *()'不支持多重查詢,'mysqli_ *'確實存在,但有複雜性在閱讀數據時,對於三個查詢,這使得它過度。 – 2013-10-24 00:39:49
@OP我認爲你的意思是'SELECT Sequence'而不是'SELECT Sequnce' –