2014-04-14 36 views
0

如何通過PDO獲取mySQL DB中的記錄數?如何通過sql語句獲取db中的記錄數?

我的代碼:(但有些事情是wrong.It不工作)

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']; 

} 

}

+0

http://stackoverflow.com/questions/15990857/reference-frequently-asked-questions-about-pdo –

+0

使用mysqli的更快,更容易 –

+3

@MustafaMJalal你錯了兩個聲明 –

回答

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'];  } 

     }