2

I have the following PHP snippet:

function getFacilities($testName, $dbAdapter) {
  $sql1 = "SELECT * FROM facilities_db WHERE facility_name = '$testName'";
  $result1 = $dbAdapter->query($sql1);

  $facility_details = array();
  while ($row = mysqli_fetch_assoc($result1))
  {
    $facility_details[] = $row;
  }
  return $facility_details;
}

$testName = "Abraham Moss Leisure";
$facility_data = getFacilities($testName, $mysqli);

The value of $testName is arbitrary just for testing purposes. This is the function from one of the answers, but my the only difference in my initial definition was not having $dbAdapter as a parameter.

9
  • 2
    Show us how you define your function!! Commented Apr 11, 2016 at 10:55
  • 2
    please post your whole function & how did you call the function with parameter. Commented Apr 11, 2016 at 10:56
  • Are you keeping the ' when using the params? Commented Apr 11, 2016 at 11:01
  • Show us how you're defining the function! Commented Apr 11, 2016 at 11:02
  • 1
    Also don't forget to check scope of all your variables with and without function!! Commented Apr 11, 2016 at 11:04

3 Answers 3

6

You need to set $testName as a parameter, but also the adapter you are going to use to get the connection to your SGBD (here it's mysqli).

function getFacilities($testName, $dbAdapter) {
    $sql1 = "SELECT * FROM facilities_db WHERE facility_name = '$testName'";
    $result1 = $dbAdapter->query($sql1);

    $facility_details = array();
    while ($row = mysqli_fetch_assoc($result1))
    {
      $facility_details[] = $row;
    }
    return $facility_details;
}

We need to pass $testName and $mysqli as your dbAdapter to the function. Thus, this is how we would call the function :

$facility_data = getFacilities($testName, $dbAdapter);

hope this'll help,

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

5 Comments

The array returned is still empty :(. If I call the function using a string instead of $testName for example, the same thing happens.
Yes. I just checked it without the function and it works like a charm.
Worked after defining $dbAdapter as a global variable with $mysqli value and only parsing the $testName (for whatever reason that is...). Thanks!
If global answer works for you - you must try to provide $dbAdapter instead of $mysqli to getFacilities call.
All right, I added the $dbAdapter parameter back and parsed it when calling the function after defining it as $mysqli.
0

test query result:

print_r(mysqli_fetch_assoc($result));

test $mysqli in function:

function get($testName, $adapter) { if (!isset($adapter)) echo 'no'; }

use static database connection:

class GlobalClass { public static $db = null; }

class Connection {
public $connection = null;
public function connect() {
    if ($connection == null)
        // operation
    $this->connection = 'obj';
}}


$db = new Connection();
$db->connect();
GlobalClass::$db = new Connection();
GlobalClass::$db->connect();
GlobalClass::$db->connection->query($sql);

function yourFunction($testName) {
    $sql1 = "SELECT * FROM facilities_db WHERE facility_name = '$testName'";

    $result1 = GlobalClass::$db->connection->query($sql1);

    $facility_details = array();
    while ($row = mysqli_fetch_assoc($result1)) {
      $facility_details[] = $row;
    }
    return $facility_details;
}

1 Comment

Thanks for the effort, but the database connection is already written and it works fine for everything else I have! Solved the problem.
-1
    function getFacilities($testName) {
          global $dbAdapter;
        $sql1 = "SELECT * FROM facilities_db WHERE facility_name = '$testName'";
        $result1 = $dbAdapter->query($sql1);

        $facility_details = array();
        while ($row = mysqli_fetch_assoc($result1))
        {
          $facility_details[] = $row;
        }
        return $facility_details;
    }

$testName = "Abraham Moss Leisure";
$facility_data = getFacilities($testName);

declare the connection variable as global and pass the value for the $testname

7 Comments

Even though I don't know why, this worked. Thank you!
omg, please, no... Why php maintainers do not removed that global yet?
You shouldn't use global variables. It leads to very high technical debt.
why cant use global variable can you plz explain then how can use the connection variable inside the function
|

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.