简体   繁体   English

在2种不同的php函数中调用MySql Select Query

[英]Call MySql Select Query in 2 different php function

Please help me with my problem.. can i call once Mysql Select Query from different function from two different function too... Sorry i don't know how to explain.. but below my sample what i want to archive.. 请帮助我解决我的问题..我也可以调用一次Mysql从两个不同的函数中选择另一个函数中的查询吗...对不起,我不知道如何解释..但是在我的示例下面,我想存档..

I want use single query for 2 function.. so maybe i have to write code like below? 我想对2函数使用单个查询..所以也许我必须编写如下代码?

function sqlSelect ($db, $id, $id2) {
   $sql = mysqli_query($db, "SELECT * FROM xxx WHERE id='$id' AND id2='$id2'");
   $row = mysqli_num_rows($sql);
   $field = mysqli_fetch_object($sql);

   return $row;

}

but how to use $field inside 2 diff function? 但是如何在2个diff函数中使用$ field?

function aaa ($a,$b,$c) {
   if($row >= 1){
      //Do something with $a $b and $c
      //$reget field with $field['column'];
      $field['column']; //???
   }

   return $result;
}

function bbb ($a,$b,$c) {
   if($row >= 1){
      //Do something with $a $b and $c
      //get field with $field['column'];
      $field['column']; //???
   }
}

what i do right now is 我现在要做的是

function aaa ($db,$id,$id2,$a,$b,$c) {
   $sql = mysqli_query($db, "SELECT * FROM xxx WHERE id='$id' AND id2='$id2'");
   $row = mysqli_num_rows($sql);
   $field = mysqli_fetch_object($sql);

   if($row >= 1){
      //Do something with $a $b and $c
      //get field with $field['column'];
   }
}

function bbb ($db,$id,$id2,$a,$b,$c) {
   $sql = mysqli_query($db, "SELECT * FROM xxx WHERE id='$id' AND id2='$id2'");
   $row = mysqli_num_rows($sql);
   $field = mysqli_fetch_object($sql);

   if($row >= 1){
      //Do something with $a $b and $c
      //get field with $field['column'];
   }
}

But if i use what i write and code right now i think it's have to call same query twice. 但是,如果我现在使用自己编写的代码,我认为必须两次调用相同的查询。 I cannot use query outside function so i have to write query on each function but i want to just write query once and can use in two different function.. 我不能在函数外使用查询,所以我必须在每个函数上写查询,但是我只想写一次查询就可以在两个不同的函数中使用。

Sorry for stupid explanation.. 对不起,愚蠢的解释。

Thank you for help 谢谢你的帮助

One implementation can be as follow by passing an array as argument, 可以通过将数组作为参数传递来实现,如下所示:

<?php
$arg['db']="database";
$arg['tabe']="table";
$arg['search']['id1']="id1";
$arg['search']['id2']="id2";
$arg['do_something']['a']="a";
$arg['do_something']['b']="b";
$arg['do_something']['c']="c";
function searchAndDoSomethingAndReturnResult($arg)
{
    $return = NULL;
    $query="SELECT * FROM ".$arg['table'];
    $flag=false;
    foreach($arg['search'] as $key=>$value)
    {
        if($flag)
            $query.=" AND ";
        else
            $flag=true;
        $query.= $key." = '".$value."' ";
    }
    $row = mysqli_num_rows($query);
    $field = mysqli_fetch_object($query);
    if($row >= 1)
    {
        foreach($arg['do_something'] as $job=>$value)
        {
            // $return[] = "some result"
            // do something 
        }
    }
    return $return;
}
?>

let me know if this solve your problem 让我知道这是否可以解决您的问题

You can either pass in the $field variable as a function argument 您可以将$field变量作为函数参数传递

<?
$field = ...;
function sql1($field,...,...){
    // Use $field here
}
function sql2($field,...,...){
    // Use $field here
}
?>

Alternatively, you can use the global keyword to access variables from outside of the function 或者,您可以使用global关键字从函数外部访问变量

<?
$field = ...;
function sql1(){
    global $field;
    // Use $field here
}
function sql2(){
    global $field;
    // Use $field here
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM