Fellow Programmers,
Read this but irrelevant:
http://php.net/manual/en/function.die.php
- Why is it not possible to add the die function to close the mysql connection after making an echo ?
<?php
//Check for username match in "Usernames" column in "users" table. If there is a match then do the following ...
$stmt = mysqli_prepare($conn, 'SELECT COUNT(*) FROM users WHERE usernames = ?');
mysqli_stmt_bind_param($stmt, 's', $_POST['member_registration_username']);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $rows);
if (mysqli_stmt_fetch($stmt) && $rows)
{
'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
die(
);
}
?>
If you add the die function before the echo then it works NOT also. Like so:
<?php
//Check for username match in "Usernames" column in "users" table. If there is a match then do the following ...
$stmt = mysqli_prepare($conn, 'SELECT COUNT(*) FROM users WHERE usernames = ?');
mysqli_stmt_bind_param($stmt, 's', $_POST['member_registration_username']);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $rows);
if (mysqli_stmt_fetch($stmt) && $rows)
{
die(
);
'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
}
?>
However, adding the echo inside the die function works! Why should you add the echo inside the die function when you want to echo a message and only then want the mysql connection to be closed ? Strange!
This works:
<?php
//Check for username match in "Usernames" column in "users" table. If there is a match then do the following ...
$stmt = mysqli_prepare($conn, 'SELECT COUNT(*) FROM users WHERE usernames = ?');
mysqli_stmt_bind_param($stmt, 's', $_POST['member_registration_username']);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $rows);
if (mysqli_stmt_fetch($stmt) && $rows)
{
die(
'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
);
}
?>
Q2. As you can see from the following code that there is an echo. But the words “echo” are not written and the code works. If you type echo however, then you get an error! Why is that ?
What works …
<?php
//Check for username match in "Usernames" column in "users" table. If there is a match then do the following ...
$stmt = mysqli_prepare($conn, 'SELECT COUNT(*) FROM users WHERE usernames = ?');
mysqli_stmt_bind_param($stmt, 's', $_POST['member_registration_username']);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $rows);
if (mysqli_stmt_fetch($stmt) && $rows)
{
die(
'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
);
}
?>
What doesn’t work but should have:
<?php
//Check for username match in "Usernames" column in "users" table. If there is a match then do the following ...
$stmt = mysqli_prepare($conn, 'SELECT COUNT(*) FROM users WHERE usernames = ?');
mysqli_stmt_bind_param($stmt, 's', $_POST['member_registration_username']);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $rows);
if (mysqli_stmt_fetch($stmt) && $rows)
{
die(
echo 'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
);
}
?>
Get error mssg:
Parse error: syntax error, unexpected ‘echo’ (T_ECHO), expecting ‘)’ in C:\xampp\htdocs\sn\reg_short.php on line …
The following doesn’t work either. Note the double quote marks:
<?php
if (mysqli_stmt_fetch($stmt) && $rows)
{
die(
echo 'That Username ".htmlspecialchars($_POST['member_registration_username'])." is already registered!'
);
}
?>
Parse error: syntax error, unexpected ‘echo’ (T_ECHO), expecting ‘)’ in C:\xampp\htdocs\sn\reg_short.php on line