php-pdo-mysql-helper-class icon indicating copy to clipboard operation
php-pdo-mysql-helper-class copied to clipboard

New functions made

Open tenq opened this issue 7 years ago • 1 comments

Hi, great job...

I have created new specific functions for my job. If you think be useful, merge it:

public function getValue($table, $col, $value, $col_returned)
	{
		$sqld = "SELECT `$col_returned` FROM `$table` WHERE $col='".$value."'";
		$q = $this->pdo->prepare($sqld);
		$q->execute();
		$f = $q->fetch();
		$result = $f[$coluna_retornada];
		return $result;
	}

    public function getMaxValue($table, $col, $value, $col_returned)
    {
        $sqld = "SELECT MAX($col_returned) as total FROM `$table` WHERE $col ='".$value."'";
        $q = $this->pdo->prepare($sqld);
        $q->execute();
        $f = $q->fetch();
        $result = $f["total"];
        return $result;
    }


    public function getTotal($table, $col, $value)
    {
        $sqld = "SELECT COUNT(*) as total FROM `$table` WHERE $col='".$value."'";
        $q = $this->pdo->prepare($sqld);
        $q->execute();
        $f = $q->fetch();
        $result = $f["total"];
        return $result;
    }

    public function getTotalWhere($table, $where)
    {

        $data = array_values( $where );
        $cols=array_keys($where);

        foreach ($cols as $key) {
          $keys=$key."=?";
          $mark[]=$keys;
        }


        $jum=count($where);
        if ($jum>1) {
            $im=implode(' and  ', $mark);
            $sqlget = "SELECT COUNT(*) as total from $table WHERE $im";
            $sel = $this->pdo->prepare($sqlget);
        } else {
          $im=implode('', $mark);
          $sqlget = "SELECT COUNT(*) as total from $table WHERE $im";
          $sel = $this->pdo->prepare($sqlget);
        }

        $sel->execute( $data );

        $f = $sel->fetch();
        $result = $f["total"];
        
        return  $result;


    }

tenq avatar Jun 03 '17 12:06 tenq

i've invited you, you can merge it yourself now

wildantea avatar Jun 06 '17 06:06 wildantea