1

It is my first time in PHP and I don't know what's wrong with this code.

Line 20 is this if statement: 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;
    }
}
3
  • Is missing a catch block. Commented Mar 23, 2017 at 18:16
  • Your error is above the given line. The system expects a catch block after try. Commented Mar 23, 2017 at 18:17
  • 1
    Marked as off-topic, since this is a simple typographical error/lack of reading the error message. Commented Mar 23, 2017 at 18:19

3 Answers 3

8

The error means that you code is missing a catch(){} block.

The syntax of try-catch is:

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.
}

You can also have a finally clause which if always executed whether an exception occurred or not syntax below:

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";
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to put a catch statement after you close the try block, like this:

<?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;
    }
} 

Comments

1

You are missing the catch block after the try block. Each try must have at least one corresponding catch or finally block in PHP. refer : http://php.net/manual/en/language.exceptions.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.