반응형
방금 데이터베이스 연결을 변경했습니다. 아직 PDO 클래스 나 OOP에 익숙하지 않습니다. 어쨌든 다음과 같이 db에 연결합니다.
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
try
{
$this->db = new PDO($dsn, DB_USER, DB_PASS);
}
catch ( Exception $e )
{
die ( $e->getMessage() );
}
이 쿼리에서 행 수를 얻으려고합니다.
$ip = $this->ip();
$sql = "SELECT `id` FROM `login_failed`
WHERE `ip` = :ip AND `time` BETWEEN NOW( ) - INTERVAL 120 MINUTE AND NOW( )
LIMIT 3";
try
{
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':ip', $ip, PDO::PARAM_STR);
$result = $stmt->execute(); // $result = true
$n = $stmt->num_rows ; // n = NULL?
$stmt->closeCursor();
}
catch (Exception $e)
{
die ($e->getMessage() );
}
phpmyadmin
에서 결과를 얻었으므로 쿼리가 정확하지만 $ n
은 어떤 이유로 NULL
입니다 .. 행 수는 어떻게 얻습니까? PDO
사용
해결 방법
마지막 SQL 문이 the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on 휴대용 애플리케이션 용.
따라서 쿼리를 변경하지 않고 행을 계산하려면 다음을 수행하십시오.
$result = $stmt->execute();
$rows = $stmt->fetchAll(); // assuming $result == true
$n = count($rows);
참조 페이지 https://stackoverflow.com/questions/5556540
반응형
'MySql' 카테고리의 다른 글
MySQL MySql 커뮤니티 서버 최소 시스템 요구 사항 (0) | 2020.10.12 |
---|---|
MySQL mysql에서 longblob과 longtext의 차이점은 무엇입니까? (0) | 2020.10.12 |
MySQL 거대한 테이블의 MySQL 최적화 (0) | 2020.10.12 |
MySQL 내 SQL에서 "int"와 "int (2)"데이터 유형의 차이점 (0) | 2020.10.12 |
MySQL의 데이터베이스에서 모든 테이블 (모두 innodb)을 강제로 자르는 방법은 무엇입니까? (0) | 2020.10.12 |
댓글