SQL Query to return just single value

adznon
adznon used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
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 KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
Pls try this...

Array[0][group2]
ste5anSenior Developer

Commented:
or

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

Open in new window

Author

Commented:
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;
                  
            }
Senior Developer
Commented:
First of all: use the [code][/code] tags (or the CODE button) to emebed code. This increases readbility.

Untested:
dbquery.php - File
 // #################################################################################
 // Database Querys
 // #################################################################################
       public function scalar($query){
                   $stmt = sqlsrv_query($this->conn,$query);
                   $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
                   return sqlsrv_get_field($row, 0);
       }
 // #################################################################################
 // 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'";
             $result = $this->scalar($query);
             echo "<pre>";print_r($result);exit;
             if($result == "Y"){
                   echo "Result is Yes";exit;
                   return $result;
             }
             else{
                   $_SESSION['error_msg'] = "You are not an $type";
                   return;
             }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial