2013-01-14 268 views
0

我希望這沒有被問到別的地方,因爲我看,但在這裏。我寫了這個函數爲我創建表。它可以工作,但是我遇到的問題是每次我調用這個函數時都會運行查詢。有沒有辦法調用這個函數並將查詢傳遞給函數?有沒有辦法將查詢傳遞給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); 
}; 
+4

您正在使用[**的**過時的數據庫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

回答

2

當然,只需添加一個新的參數(如$awardname)到您的功能及使用方法,當你調用「的mysql_query()`。

也被告知,使用mysql_*功能,強烈建議:

這個擴展不贊成PHP 5.5.0,並且將在 將來被移除。相反,應該使用MySQLi或PDO_MySQL擴展。

http://www.php.net/mysql_query

0

在這裏你去

function createawardtables($result, $awardname, $awardshortname, $maxawards, $id) 
{  
    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); 
}; 

和函數調用

$query="SELECT * FROM awards WHERE id = $id"; 
$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); 
createawardtables($result, $awardname, $awardshortname, $maxawards, $id); 
+0

感謝您的幫助我嘗試過類似的東西,但我用call_user_func – Warmour

相關問題