2013-10-24 52 views
0

我想減少這個頁面連接的數量。我讀過的所有內容都表明,只有一個連接應該足夠,但如果我刪除了其他連接,則該頁面不會連接到我的服務器,併爲我提供我期待的結果。也是我正在使用兩次的查詢之一,但如果我第二次調用原始查詢,它也不起作用。如何減少連接到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> 
+0

你可以做的(全部獨家)移動到'MySQLi_'並使用['mysqli_multi_query()'](http://php.net/manual/en/ mysqli.multi-query.php)我只是不確定該函數是否存在MySQL_' - 我注意到你正在使用'MySQL_'和'MySQLi_'刪除'MySQL_'並使用/傳遞'$ con'來您的查詢。這樣你只能使用一個連接(呼叫)。即:'$ query = $ con> query(「SELECT * FROM ...' –

+0

@ Fred-ii-'mysql _ *()'不支持多重查詢,'mysqli_ *'確實存在,但有複雜性在閱讀數據時,對於三個查詢,這使得它過度。 – 2013-10-24 00:39:49

+0

@OP我認爲你的意思是'SELECT Sequence'而不是'SELECT Sequnce' –

回答

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> 
+0

'$ query =「SELECT序列,mcacctname FROM'elo';」;';-) –

+0

實際上,OP做了初始錯字 –

+0

OP的錯字,但是修正了 – 2013-10-24 00:42:15