setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //query to count rows $sql = "SELECT COUNT(*) FROM tablename"; //execute query $result = $conn->query($sql); //fetch result $row = $result->fetch(PDO::FETCH_NUM); //display count echo "Total number of rows: " . $row[0]; //close connection $conn = null; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?>Package/Library: Both examples use the built-in PHP MySQL extensions and do not require any additional package or library.