简体   繁体   中英

SQL query in PHP only works outside function

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.

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,

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;
}
    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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