我目前正在尝试在try / catch中实现if语句。
我的代码如下:
let refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
viewDidLoadMethods()
refreshControl.tintColor = .black
refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
collectionView.addSubview(refreshControl)
collectionView.alwaysBounceHorizontal = true
体内看起来如下:
require_once(__DIR__."/../includes/config.php");
require_once(__DIR__."/../includes/functions.php");
startSession();
define('DSN','mysql:host=localhost;dbname=solvedesktop');
define('USERNAME' , 'root');
define('PASSWORD' , '');
try {
$DB = new PDO(DSN, USERNAME, PASSWORD);
} catch(PDOException $e) {
echo $e->getMessage();
}
try {
if(isset($_SESSION["id"])){
$sql = "SELECT * FROM drop_down WHERE published = 1 AND loggedin = 1";
$result = $DB->query($sql);
$row_count = $result->rowCount();
if($row_count){
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
}
} else {
$sql = "SELECT * FROM drop_down WHERE published = 1 AND loggedin = 0";
$result = $DB->query($sql);
$row_count = $result->rowCount();
if($row_count){
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
}
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
最初,我有一个简单的try / catch,其中页面只需要发布即可在前端可读,但是自那以后,我决定只为用户保护某些页面,因此我需要实现如果try / catch中的if语句,我不认为我距离不太远(只要可以做到),但是我正在努力完成我网站的这一部分。