カテゴリー
db

Laminas dbの例(fetchOne的)laminas dbのselectを使う

itemテーブルの列数を取る

<?php
//autoloadの位置は適宜設定してください
require '../vendor/autoload.php';

use Laminas\Db\Adapter\Adapter;
use Laminas\Db\Sql\Sql;

$db = new Adapter([
     'driver'   => 'Pgsql', //postgresを使う場合
     'host' => 'localhost',
     'database' => db名,
     'username' => ユーザID,
     'password' => パスワード
 ]);
$sql = new Sql($db);
$sel = $sql->select();
$sel->from('item');
$sel->columns(['num' => new \Laminas\Db\Sql\Expression('COUNT(*)')]);
$stm = $sql->prepareStatementForSqlObject($sel);
echo $stm->execute()->current()['num'];
カテゴリー
db

Laminas dbの例(fetchOne的)sqlベタ書き

テーブルの列数を取る例

<?php
//autoloadの位置は適宜修正してください
require '../vendor/autoload.php';

use Laminas\Db\Adapter\Adapter;

$db = new Adapter([
     'driver'   => 'Pgsql', //postgresを使う場合
     'host' => 'localhost',
     'database' => db名,
     'username' => ユーザID,
     'password' => パスワード
 ]);
 $stm = $db->createStatement("SELECT COUNT(*) FROM item");
 echo $stm->execute()->current()['count'];