有没有办法将查询传递给php函数

有没有办法将查询传递给php函数,php,mysql,Php,Mysql,我希望这个问题没有被问到其他地方,因为我看了,但我看了。我编写这个函数是为了为自己创建表。它可以工作,但我遇到的问题是,每次我调用这个函数时,它都会运行查询。是否有方法调用此函数并将查询传递给该函数 function createawardtables($awardname, $awardshortname, $maxawards, $id){ $query="SELECT * FROM awards WHERE id = $id"; $result = mysql_qu

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

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); 
};
函数createawardtables($awardname、$awardshortname、$maxawards、$id){
$query=“从id=$id的奖励中选择*”;
$result=mysql\u query($query)或die(“SQL查询有问题:”.mysql\u error());
while($row=mysql\u fetch\u数组($result)){
$order=数组(“,”1“,”2“,”3“,”4“,”5“,”6“,”7“,”8“,”9“,”10”);
回显“$awardfullname订单”;

对于($i=1;$i当然,只需在函数中添加一个新参数(如
$awardname
),并在调用“mysql\u query()”时使用它

另外,强烈建议不要使用
mysql.*
函数:

从PHP5.5.0开始,此扩展已被弃用,并将在中删除 相反,应该使用MySQLi或PDO_MySQL扩展

给你

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); 
};

您正在使用并且应该使用。您还(取决于
$id
来自何处)容易受到现代API的攻击,这将使您更容易使用。感谢您的帮助我尝试了类似的方法,但我使用了call_user_func进行了尝试
$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);