0

我希望这没有在其他地方被问到,因为我看了,但这里有。我编写了这个函数来为我创建表。它可以工作,但我遇到的问题是每次我调用这个函数时它都会运行查询。有没有办法调用这个函数并将查询传递给函数?

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

2 回答 2

2

当然,只需在$awardname函数中添加一个新参数(如 )并在调用 'mysql_query()` 时使用它。

另请注意,强烈建议不要使用mysql_*函数:

此扩展自 PHP 5.5.0 起已弃用,并将在未来删除。相反,应该使用 MySQLi 或 PDO_MySQL 扩展。

http://www.php.net/mysql_query

于 2013-01-14T10:46:52.873 回答
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);
于 2013-01-14T10:47:26.120 回答