#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    70
    Rep Power
    1

    Question Why Can't Die Function Be Written before Or After An Echo ?


    Fellow Programmers,

    Read this but irrelevant:

    PHP: die - Manual


    1. Why is it not possible to add the die function to close the mysql connection after making an echo ?

    PHP Code:

    <?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 Code:

    <?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 Code:

    <?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 Code:

    <?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 Code:

    <?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 Code:

    <?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 ...
    Last edited by UniqueIdeaMan; April 16th, 2017 at 04:32 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    70
    Rep Power
    1
    I know it is supposed to be like this:

    PHP Code:

    ?php >
    if (
    mysqli_stmt_fetch($stmt) && $rows
            {
                die(
                
    'That Username '.htmlspecialchars($_POST['member_registration_username']).' is already registered!'
                
    );
            }

    ?> 
    But my question was why should the echoed mssg be inside the die function ? Why can't the die function come prior or after the echo ?
    Anyway, I got my answer from another source now.
    But still, THANK YOU VERY MUCH for your reply and time in checking this issue out.
    I might aswell follow you on twitter and facebook, if you don't mind.

    I also have been brought to my sense why the following won't work:

    PHP Code:

    <?php 

    if (mysqli_stmt_fetch($stmt) && $rows
            {
                die(
                echo 
    'That Username ".htmlspecialchars($_POST['member_registration_username'])." is already registered!'
                
    );
            }

    ?>
    I can't add a double quote inside a single quote. can't expect the variable to be translated inside a string.
    I've been now told the following now from another source:

    "die() is a script killer. Any message / html output that you want printed before it DIEs can be put within the () like a normal function call.

    That is the way it is supposed to work.

    OTherwise you could use the echo function itself - echo 'Terminated'; but then you'd call die or exit with no parameters.

    In short, die is a function that echos text that is sent via a parameter in its function call. It then completely terminates the script."

    This thread can be closed/locked now.
  4. #3
  5. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    15,976
    Rep Power
    9615
    You clearly don't understand the basics of PHP. Find a good tutorial and start over.

IMN logo majestic logo threadwatch logo seochat tools logo