如何通过sql语句获取db中的记录数?

时间:2014-04-14 05:07:43

标签: php mysql sql pdo

如何通过PDO获取mySQL数据库中的记录数?。

我的代码:(但有些事情是错的。它无法正常工作。)

    function AlandakiDegerlerToplami ($tablo,$alan) 

 {  

     $dsn = 'mysql:host='.$host.';dbname='.$veritabaniadi;

    try {
        $this->conn = new PDO($dsn, $kullaniciadi, $sifre);
    } catch (PDOException $e) {
        echo vthatasi .':'. $e->getMessage();
    }

    $veriler = $this->conn->prepare('SELECT sum('.$alan.') as adet from '.$tablo);
    $toplam=$veriler->execute();

    return $toplam['adet'];
 }


      function TablodakiKayitSayisi ($tablo) 

 {  

     $dsn = 'mysql:host='.$host.';dbname='.$veritabaniadi;

    try {
        $this->conn = new PDO($dsn, $kullaniciadi, $sifre);
    } catch (PDOException $e) {
        echo vthatasi .':'. $e->getMessage();
    }

$veriler = $this->conn->prepare('SELECT count(*) as adet from '.$tablo);
        $toplam=$veriler->execute();

    return $toplam['adet'];

 }

}

1 个答案:

答案 0 :(得分:0)

最后我解决了我的问题。

代码:

            function AlandakiDegerlerToplamiWHERE ($tablo,$alan,$where) 

         {  

$kullaniciadi='bla';
$sifre='1234';
$veritabaniadi='deneme'; 

 $dsn = 'mysql:host='.$host.';dbname='.$veritabaniadi;

    try {
        $baglan = new PDO($dsn, $kullaniciadi, $sifre);
    } catch (PDOException $e) {
        echo vthatasi .':'. $e->getMessage();
    }
            $veriler = $baglan->query('SELECT sum('.$alan.') as adet from '.$tablo.' WHERE '.$where);

            foreach($veriler as $row) {
            return $row['adet'];      }
         }








            function TablodakiKayitSayisi ($tablo) 

         {


    $kullaniciadi='bla';
$sifre='1234';
$veritabaniadi='deneme'; 
 $dsn = 'mysql:host='.$host.';dbname='.$veritabaniadi;

    try {
        $baglan = new PDO($dsn, $kullaniciadi, $sifre);
    } catch (PDOException $e) {
        echo vthatasi .':'. $e->getMessage();
    }

              $veriler = $baglan->query('SELECT count(*) as adet from '.$tablo);
              foreach($veriler as $row) {
              return $row['adet'];      }

         }