PHP一些重要函數

Published
July 3, 2022
浏览次数 :  0

設置時區:

define('TIMEZONE', 'Asia/Shanghai');
date_default_timezone_set(TIMEZONE);

string轉換為time

$timestamp = strtotime($date);

sql的prepare方法:

$sql2 = "SELECT name,username,p_p,last_seen FROM users WHERE user_id=?";

        $stmt2 = $conn->prepare($sql2);
        $stmt2->execute([$conversation['user_2']]);

獲取sql查詢所有内容方法

$allConversations = $stmt2->fetchAll();

session destroy方法

session_start();
session_unset();
session_destroy();
header("Location:index.php");

pdo鏈接數據庫方式

$sName = 'localhost';

$uName = 'root';

$pass = 'root';

$dbName = 'wordpress';

//create database connection
try {
  $conn = new PDO("mysql:host=$sName;dbname=$dbName",$uName,$pass);
  $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  
}catch(PDOException $e) {
  echo "Connection Failed: " . $e->getMessage();

}

驗證查詢結果存在的方式:

 if ($stmt->rowCount() > 0) {
      $em = "The username ($username) is taken!";
      header("Location:../../sign.php?error=$em&$data");
      exit;
    }
//获取url request ?error部分
$_GET['error'] == $em 

Top