这是我第一次使用PHP而且我不知道这段代码有什么问题。
第20行是if语句:if (isset($_COOKIE['hash'])) {
<?php
if (!isset($_GET['page'])) {
header('Location: /main');
exit();
}
ini_set('display_errors','Off');
try {
$database_host = "localhost";
$database_name = "NAME";
$database_user = "USER"; //name for phpMyAdmin in bplaced
$database_pass = "*******"; //password in phpMyAdmin in bplaced
global $db;
$db = mysqli_connect($database_host, $database_user, $database_pass, $database_name) or
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
mysqli_set_charset($db, "utf8");
}
if (isset($_COOKIE['hash'])) {
$sql = $db->query("SELECT * FROM `users` WHERE `hash` = " . $db->quote($_COOKIE['hash']));
if ($sql->rowCount() != 0) {
$row = $sql->fetch();
$user = $row;
}
}
答案 0 :(得分:4)
错误表示您的代码缺少catch(){}
块。
try-catch的语法是:
try
{
//Do something here which might cause an exception
}
catch(Exception $e)
{
//You are here means that the exception occurred now do something else here.
}
你也可以有一个finally子句,如果总是执行是否发生异常,或者不是下面的语法:
try
{
print "this is our try block\n";
throw new Exception();
}
catch (Exception $e)
{
print "something went wrong\n";
}
finally
{
print "This part is always executed\n";
}
答案 1 :(得分:1)
关闭try块后需要输入一个catch语句,如下所示:
<?php
if (!isset($_GET['page'])) {
header('Location: /main');
exit();
}
ini_set('display_errors', 'Off');
try {
$database_host = "localhost";
$database_name = "NAME";
$database_user = "USER"; //name for phpMyAdmin in bplaced
$database_pass = "*******"; //password in phpMyAdmin in bplaced
global $db;
$db = mysqli_connect($database_host, $database_user, $database_pass, $database_name) or die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
mysqli_set_charset($db, "utf8");
}
catch (Exception $e) {
// do something
}
if (isset($_COOKIE['hash'])) {
$sql = $db->query("SELECT * FROM `users` WHERE `hash` = " . $db->quote($_COOKIE['hash']));
if ($sql->rowCount() != 0) {
$row = $sql->fetch();
$user = $row;
}
}
答案 2 :(得分:0)
在try块之后你缺少catch块。每次尝试必须在PHP中至少有一个相应的catch或finally块。 参考:http://php.net/manual/en/language.exceptions.php