Basic SQL Select Interface

Hello All,


public static function whereHelper($whereClause)
  {
  $strData = 'WHERE ';
  $tempNum = 0;
  foreach($whereClause as  $key => $value)
  {
  if ( $tempNum == 1){
  
  $strData  =  $strData . ' ' . "AND" . $key . "=" . $value;
  }
  else
  {
  $strData  =  $strData . ' ' . $key . "=" . $value;
  $tempNum = 1;
  }
  }
  return $strData;
  }
  
  public static function statementHelper($value)
  {
  $tempStore = "";
  $tempNum = 0;
  foreach($value as $statementValue)
  {
  
  if($tempNum == 0 )
  {
  $tempStore =  $statementValue;
  $tempNum = 1;
  
  }
  else 
  {
     $tempStore = $tempStore . ' , ' . $statementValue;
     }
     }
  return $tempStore;
  }



public static function selectStatement($arrayValueClause)
  {
  $fieldStatement = "*";
  $whereStatement = "";
    foreach($arrayValueClause as  $key => $value)
    {
    if ($key == "statement")
    {
       $fieldStatement = self::statementHelper($value);
    }
    if ($key == "where")
    {
    $whereStatment = self::whereHelper($value);
    }
    }
    
    return   self::executeSelect('SELECT ' .$fieldStatement. ' FROM '. self::$table . ' '. $whereStatment);
    }
  
    public static function update($colValueArray, $whereClause)
    {
    }
  public static function executeSelect($sql)
 {
  //return whereHelper();
try
   {
   //$dhn = new PDO(self::$dsn, self::$user, self::$password);
   
   //print_r($data_array);
   self::$dhn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  $tmt = self::$dhn->prepare($sql);
 
   $tmt->execute((array)self::$data_array);
   
   $row = $tmt->fetchAll(PDO::FETCH_ASSOC);
   return $row;
   }
   catch(PDOException $e)
   {
   echo 'Connection failed: ' . $e->getMessage();
   }
   }

The above code works but I would like to improve on it. Please review it and comment.

Regards,
Janus

Yes, indent the code better :slight_smile:

That is some convoluted stuff – what is your perceived benefit of that exactly?

Oddz,

I could use it to do the following;
$myQueryValue = array(“statement” => array(“user_name”, “user_email”), “where” =>array(“id” => 7));
$returnQuery = Table(“users”)->selectStatement($myQueryValue);

I would need your review and suggestion.

Regards,
Janus


public static function whereHelper($whereClause)
     {
       $strData = 'WHERE ';
       $tempNum = 0;
       foreach($whereClause as  $key => $value)
                 {
                   if ( $tempNum == 1){
  
                      $strData  =  $strData . ' ' . "AND" . $key . "=" . $value;
                      }
                   else
                      {
                          $strData  =  $strData . ' ' . $key . "=" . $value;
                          $tempNum = 1;
                      }
                  }
             return $strData;
     }
  
  public static function statementHelper($value)
                      {
                         $tempStore = "";
                         $tempNum = 0;
                         foreach($value as $statementValue)
                                     {
  
                                        if($tempNum == 0 )
                                            {
                                              $tempStore =  $statementValue;
                                              $tempNum = 1;
  
                                            }
                                        else 
                                            {
                                               $tempStore = $tempStore . ' , ' . $statementValue;
                                             }
                                   }
                                     return $tempStore;
                  }



public static function selectStatement($arrayValueClause)
                              {
                                    $fieldStatement = "*";
                                    $whereStatement = "";
                                    foreach($arrayValueClause as  $key => $value)
                                              {
                                                if ($key == "statement")
                                                   {
                                                      $fieldStatement = self::statementHelper($value);
                                                   }
                                               if ($key == "where")
                                                  {
                                                     $whereStatment = self::whereHelper($value);
                                                   }
                                               }
    
                              return   self::executeSelect('SELECT ' .$fieldStatement. ' FROM '. self::$table . ' '. $whereStatment);
    }
  
 
  public static function executeSelect($sql)
                                 {
  //return whereHelper();
                                       try
                                            {
   //$dhn = new PDO(self::$dsn, self::$user, self::$password);
   
   //print_r($data_array);
                                             self::$dhn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
                                            $tmt = self::$dhn->prepare($sql);
 
                                            $tmt->execute((array)self::$data_array);
   
                                            $row = $tmt->fetchAll(PDO::FETCH_ASSOC);
                                            return $row;
                                            }
                                      catch(PDOException $e)
                                                {
                                                     echo 'Connection failed: ' . $e->getMessage();
                                      }
   }  

Teddy , Oddz and All
I didn’t highlight Table constructor because it just assign value to self::$table, which is the table name. self::$data_array has nothing assigned to it.

Regards,
Janus