PHP と MySQL でテーブルの行数を調べるには以下のようにします。
1 2 3 4 | $sql = 'select count(*) as cnt from テーブル名'; $res = mysql_query($sql); $row = mysql_fetch_assoc($res); echo $row['cnt']; |
テーブル名のところに調べたいテーブル名を入れてください。
関数にしちゃうと便利かもしれませんね。
1 2 3 4 5 6 | function table_col($str) { $sql = 'select count(*) as cnt from '.$str; $res = mysql_query($sql); $row = mysql_fetch_assoc($res); return $row['cnt']; } |
引数にテーブル名を入れれば行数が返ってきますね。便利ですね!
コメント