Avatar of adznon
adznon
 asked on

SQL Query to return just single value

I have a database setup for authorisation to a website, using PHP i can get the response below

Name / Group1 / Group2 / Group3
Me / Y / N / Y

Query I am using is :-

Select [Group2] from Table where Name = 'Me'

but this is returning

Array
(
    [0] => Array
        (
            [group2] => Y
        )

)

I am looking for it to just return Y
DatabasesPHPSQL

Avatar of undefined
Last Comment
ste5an

8/22/2022 - Mon
ste5an

hmm, it depends on how you query your database and what framework (methods) you use. Using query/fetch returns a row set, thus the arrays. When you need a scalar you need to return the column:

e.g. something like

$query = $connection->query("Select [Group2] from Table where Name = 'Me'");
$rowset = $query->fetch();
$scalar = $rowset["Group2"];

Open in new window

Pawan Kumar

Pls try this...

Array[0][group2]
ste5an

or

$query = $connection->query("Select [Group2] from Table where Name = 'Me'");
$scalar = $query->fetchColumn();

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
adznon

ASKER
Sorry i a bit at a loss how to add it in. This is the full code that i am using that returns the before mentioned array but needs to bring back just a Y to the String $checkaccess

admin.php - File
$checkaccess = $dh->getUserData('admin');

dbquery.php - File
// #################################################################################
// Database Querys
// #################################################################################
      public function select($query){
                  $stmt = sqlsrv_query($this->conn,$query);
                  $result = array();
                  while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
                        //echo "<pre>";print_r($row);exit;
                        $result[] = $row;
                  }
                  return $result;
      }
      public function query($query){
                  $stmt = sqlsrv_query($this->conn,$query);
                  return $stmt;
      }
// #################################################################################
// Access Check
// #################################################################################
      public function getUserData($type){
            $tpxid = "";
            $tpxid = trim(substr($_SERVER['AUTH_USER'], strrpos($_SERVER['AUTH_USER'], '\\') + 1));
            $query = "  Select [admin] from [Users] where TPXid = '$tpxid'";
            //echo $query;exit;
            $result = $this->select($query);
            echo "<pre>";print_r($result);exit;
            //echo $result;exit;
            if($result == "Y"){
                  //echo $result."<BR>";
                  echo "Result is Yes";exit;
                  return $result;      
            }
            else{
                  //echo "Result is No";Exit;
                  $_SESSION['error_msg'] = "You are not an $type";
                  return;
                  
            }
ASKER CERTIFIED SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.