0

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..

I want use single query for 2 function.. so maybe i have to write code like below?

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?

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

5
  • so put the query itself into ANOTHER function and have aaa() and bbb() call that function as well. Commented Jul 7, 2015 at 16:55
  • isn't same call query twice? same as i do right now? Commented Jul 7, 2015 at 17:00
  • and how i get return $field? Commented Jul 7, 2015 at 17:01
  • 1
    you don't. you return the entire row.... Commented Jul 7, 2015 at 17:11
  • Maybe i just use array for that.. thanks for response.. Commented Jul 7, 2015 at 17:19

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answer and remind me that can use array for return.. i just forgot that.. however your answer is great but i can't implemented into my code because will use diff way and place.. But thanks again.. because your answer i just remember to use array.. God Bless You
0

You can either pass in the $field variable as a function argument

<?
$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

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

1 Comment

I don't like use global but thanks anyway for response..God Bless You

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.