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.

arch1ve
  • 185
  • 1
  • 11

3 Answers3

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,

Unex
  • 1,710
  • 11
  • 16
  • The array returned is still empty :(. If I call the function using a string instead of $testName for example, the same thing happens. – arch1ve Apr 11 '16 at 11:24
  • Yes. I just checked it without the function and it works like a charm. – arch1ve Apr 11 '16 at 11:43
  • Worked after defining $dbAdapter as a global variable with $mysqli value and only parsing the $testName (for whatever reason that is...). Thanks! – arch1ve Apr 11 '16 at 11:59
  • If `global` answer works for you - you must try to provide `$dbAdapter` instead of `$mysqli` to `getFacilities` call. – vp_arth Apr 11 '16 at 12:03
  • All right, I added the $dbAdapter parameter back and parsed it when calling the function after defining it as $mysqli. – arch1ve Apr 11 '16 at 12:13
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;
}
Dobe Lee
  • 465
  • 2
  • 12
  • Thanks for the effort, but the database connection is already written and it works fine for everything else I have! Solved the problem. – arch1ve Apr 11 '16 at 12:17
-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

Arun Kumaresh
  • 5,734
  • 5
  • 28
  • 45