一个小问题,如果没有设置Get-id,我将如何使用我的会话id。这是我的一小段代码。
if (isset($_GET['id']) && is_numeric($_GET['id']) && intval($_GET['id'])) {
$id = $_GET['id'];//the user id
} else {
$id = $_SESSION['id'];//the user id
}
现在,如果?根本没有设置id=,但如果它为空,则会抛出错误
SQL:select*from users where id=>>您的SQL语法有错误;查看与MySQL服务器版本对应的手册,了解在第1行"附近使用的正确语法
我只是想让它不失败,只有当它是一个实际的链接,比如profile_pic.php时,它才会使用ID GET?id=100,但在其profile_pic.php时使用会话?id=或profile_pic.php?id=苹果
intval
返回的是一个整数,而不是布尔值。如果它的计算结果为0,则表示false,否则为true。不要在if.中使用它
// GET id is set and not empty and is numeric
if (isset($_GET['id']) && !empty($_GET['id']) && is_numeric($_GET['id'])) {
$id = intval($_GET['id']);
}
// Else take value from session, if not empty and is numeric
elseif (isset($_SESSION['id']) && !empty($_SESSION['id']) && is_numeric($_SESSION['id'])) {
$id = intval($_SESSION['id']);
}
// Something went wrong
else {
throw new InvalidArgumentException('id not set via GET and id not in session or not numeric');
}