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

标签 php mysql sql pdo

如何通过 PDO 获取 mySQL DB 中的记录数?

我的代码:(但是出了点问题。它不起作用。)

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

 }

}

最佳答案

终于解决了我的问题。

代码:

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

         }

关于php - 如何通过sql语句获取db中的记录数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23052334/

相关文章:

用于变更检测的 MySQL 触发器

php - 如何组合数组行并使其可变

php - 使用 Internet Explorer 时 MySQL 查询有时会失败

sql - Athena 中的加载和 SQL 查询 DynamoDB 映射数据类型

php - 从终端使用 XDebug(仅获得 SSH 访问权限)

php - 将字符串拆分为 3 个随机长度部分

mysql - 两张InnoDB表Join花了一天时间!!我无法使用 MYISAM,因为我的表超出了 MaxData 长度。如何解决这个问题呢?

c# - 来自 Access ODBC 驱动程序的无效日期时间格式异常

php - 修复sql查询(更新mysql之前工作)

php - 如何为 php-jwt 生成 key 对?