如何将参数传递给mysql中的存储函数?

时间:2016-08-10 07:52:52

标签: php mysql

我有一个存储在MySQL数据库中的函数:

    CREATE OR REPLACE FUNCTION ORDINACIJA.calcBMI
(
    cPol     varchar2,
    dPregled date,
    dRodjen  date,
    nVisina  number,
    nTezina  number
) return varchar2 is
nBMI         number;
nStarost     number;
cBMIText     varchar2(100);
nPothranjen  number;
nMrsav       number;
nNormalan    number;
nGojazan     number;
nPrekomjeran number;
nMinTezina   number;
nMaxTezina   number;
begin

    nBMI := round(nTezina/((nVisina/100)*(nVisina/100)),0);
    nStarost := round(months_between(dPregled,dRodjen),0);

    if nStarost>228 -- 19 godina
    then
        nPothranjen  := 14;
        nMrsav       := 18;
        nNormalan    := 25;
        nGojazan     := 30;
        nPrekomjeran := 35;
    else
        select per_3, per_15, per_85, per_97, per_99
        into nPothranjen, nMrsav, nNormalan, nGojazan, nPrekomjeran
        from percentile
        where pol=cPol and mjesec=nStarost;
    end if;

    nMinTezina := ceil(nMrsav*(nVisina/100)*(nVisina/100));
    nMaxTezina := floor(nNormalan*(nVisina/100)*(nVisina/100));

    if (nPothranjen>0 and nBMI<=nPothranjen)
    then
        cBMIText := 'Ekstremna pothranjenost';
    elsif (nBMI>nPothranjen and nBMI<=nMrsav)
    then
        cBMIText := 'Pothranjenost';
    elsif (nBMI>nMrsav and nBMI<=nNormalan)
    then
        cBMIText := 'Normalna tjesna masa';
    elsif (nBMI>nNormalan and nBMI<=nGojazan)
    then
        cBMIText := 'Prekomjerna tjesna masa';
    elsif (nBMI>nGojazan)
    then
        cBMIText := 'Gojaznost (pretilost)';
    else
        cBMIText := '?????';
    end if;
    if nStarost>72
    then
        cBMIText := cBMIText || ', idealno ' || nMinTezina || '-' || nMaxTezina ||'kg';
    end if;

    return(cBMIText);

exception when others then return(null);
end calcBMI;
/

我不知道如何将参数传递给此函数,因此可以计算BMI。我已成功使用此PHP代码从表中选择所有信息:

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$query = "SELECT * FROM PERCENTILE";
if ($result = mysqli_query($conn , $query)) {

    echo("<div class = 'data_wrapper'>");

    // Display Header of the table

    $fieldcount=mysqli_num_fields($result); //value = number of columns

    $row = mysqli_fetch_assoc($result);     //Fetch a result row as an associative array:

        //array to string conversion

        echo("<table id='example' class='table table-striped table-bordered' cellspacing='0' width='100%'>");

        echo("<thead> <tr>");

        foreach($row as $item){

            echo "<th>" .$item. "</th>";
        }

        echo("</tr> </thead>");


        //Footer
        echo("<tfoot> <tr>");

        foreach($row as $item){

            echo "<th>" .$item. "</th>";
        }

        echo("</tr> </tfoot>");



        //Display Data within the table
        echo("<tbody>");
        while ($row = mysqli_fetch_assoc($result)){
                echo "<tr>";
                foreach ($row as $item){
                    echo "<td contenteditable = 'true'>" . $item . "</td>"; //Change contenteditable later
                    //Editable data should be constricted, int = numbers only, string = words, date = date
            }
            echo "</tr>";
        }
        echo("<tbody>");
        echo "</table>";
        echo("</div>");

    }
/* close connection */
$mysqli->close();

但我无法弄清楚如何将pass用户参数传递给该函数。请帮忙!

非常感谢!

1 个答案:

答案 0 :(得分:0)

您只需准备一个像这部分一样的查询字符串:

$query = "SELECT * FROM PERCENTILE";

在您的情况下,您需要有变量来保存这些输入参数:

$cPol;
$dPregled;
$dRodjen;
$nVisina;
$nTezina;

然后准备查询字符串:

$query = "CALL calcBMI( '$cPol' , '$dPregled' , '$dRodjen' , '$nVisina' , '$nTezina' )";