我希望這沒有被問到別的地方,因爲我看,但在這裏。我寫了這個函數爲我創建表。它可以工作,但是我遇到的問題是每次我調用這個函數時都會運行查詢。有沒有辦法調用這個函數並將查詢傳遞給函數?有沒有辦法將查詢傳遞給PHP函數
function createawardtables($awardname, $awardshortname, $maxawards, $id){
$query="SELECT * FROM awards WHERE id = $id";
$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error());
while($row = mysql_fetch_array($result)){
$order = array("","1st","2nd","3rd","4th","5th","6th","7th","8th",'9th',"10th");
echo "<table><th colspan=4><font color=maroon size='4pt'><u><b>Orders of the $awardfullname</b></u></font></th>";
for($i=1; $i<$maxawards+1; $i++) {
${$awardshortname.$i} = dateconvert(($row["$awardshortname$i"]), 2);
${$awardshortname.$i.'by'} = $row["$awardshortname{$i}by"];
${$awardshortname.$i.'memo'} = $row["$awardshortname{$i}memo"];
echo "<tr>
<td>$order[$i] Order given on </td>
<td><input type=text name='$awardshortname$i' title='mm/dd/yyyy' value= '${$awardshortname.$i}' size=8/></td>
<td>by <input type=text name='$awardshortname{$i}by' title='Name of who gave the award.' value='${$awardshortname.$i.'by'}'size=15/></td>
<td>for <input type=text name='$awardshortname{$i}memo' title='What the award was give for.' value='${$awardshortname.$i.'memo'}'size=15/></td>
</tr>";
if($i==10 or $awardshortname=="initiate"){
echo "</table><br />";
};
};
};
mysql_free_result($result);
};
您正在使用[**的**過時的數據庫API(HTTP://計算器。 com/q/12859942/19068),並應使用[現代替代](http://php.net/manual/en/mysqlinfo.api.choosing.php)。你也是(取決於'$ id'來自哪裏)**容易受到[SQL注入攻擊](http://bobby-tables.com/)**現代API會使[防禦]更容易http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己從。 – Quentin