Version 2: Display visitor's number on your web page (changed code after getting answer for first version).
The first version is here: Display visitor's number on your web page
The "visitors" table has only one column (visitor_count) and only one row. The column's initial value is 0.
The new code is below:
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Visitor Count</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "exp";
$internal_error_code = 0;
try {
$internal_error_code = 1;
// create db connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// check db connection
if ($conn == false) {
throw new Exception("Connection failed: " . mysqli_connect_error());
}
$internal_error_code = 2;
// execute query
$sql = "UPDATE visitors SET visitor_count = visitor_count + 1";
$result = mysqli_query($conn, $sql);
// check result
if ($result == false) {
throw new Exception("Query failed: " . $sql. "<br>". "Error: " . mysqli_error($conn));
}
$internal_error_code = 3;
// execute query
$sql = "SELECT visitor_count FROM visitors";
$result = mysqli_query($conn, $sql);
// check result
if ($result == false) {
throw new Exception("Query failed: " . $sql. "<br>". "Error: " . mysqli_error($conn));
}
$internal_error_code = 4;
// get number of rows
$num_rows_expected = 1;
$num_rows_got = mysqli_num_rows($result);
// verify number of rows got
if ($num_rows_got != $num_rows_expected) {
throw new Exception("Error: Mismatch: Number of rows expected = " . $num_rows_expected . ", Number of rows got = " . $num_rows_got);
}
$internal_error_code = 5;
// get a row
$row = mysqli_fetch_assoc($result);
// check if we got a row
if (($row == false) || ($row == null)) {
throw new Exception("Error: Got no row (data).");
}
// display successful result
echo ("You are visitor number: " . $row["visitor_count"]);
// free the memory used by $result
mysqli_free_result($result);
// close db connection
mysqli_close($conn);
} catch (Exception $e) {
if ($internal_error_code >= 4) {
// free the memory used by $result
mysqli_free_result($result);
}
if ($internal_error_code > 1) {
// close db connection
mysqli_close($conn);
}
echo ("Error: Some error occurred. Internal error code = " . $internal_error_code);
} // end of try-catch block
?>
<!-- Rest of HTML should show up in all cases (successful cases as well as in error cases) -->
<br><br>Rest of HTML
</body>
</html>
$internal_error_code
hardly serves an useful purpose, you don't use mysqli exceptions at all (why?), and nothing is done with the messages in the exceptions. In other words, you don't use$e->getMessage()
in the catch block. I guess the latter is for security purposes, like I advised, but you still need to do something with the error message itself. You are using exceptions, but in a quite different way. \$\endgroup\$?
, or omit the text entirely. ~90% of your script can be safely deleted. You might just execute the two queries and move on. No conditions and no connection closing. \$\endgroup\$