通过电子邮件向用户发送忘记的用户名

I have some php code where I want a user to enter their email address, check it against the database then send the username associated with that email address to the user. Basically a 'forgot username' feature.

I have the following code so far, which will send en email but inside the email it reads Your username is: And that's it. The $username variable isn't being passed to the email for some reason. I am new enough to PHP so any help would be greatly appreciated.

<?php

if(isset($_POST['submit'])){

    $email = $_POST['email'];

    if($email==''){
    echo "<script>alert('Please enter email address!')</script>";
    exit();
    }

    $sql=mysql_query("select user_name from users where user_email='$email'");
     if(mysql_num_rows($sql)>=1)
       {
        //echo "<script>alert('Username $user_name already exist in our database, please try another one!')</script>";
        $username = $row[0];


        $subject = 'Forgotten Username';
        $message = 'Your username is: '.$username.'';
        $header = 'Header';

        if($_POST){    
        mail($email, $subject, $message, $header);
        $feedback = 'Thanks for contacting us! We will be in contact soon!';
            }
       }
     else {
     echo "<script>alert('Email doesn't exist')</script>";
     }
}
?>

HTML

<form method='post' action='forgotusername.php'>
        <table width='400' border='5' align='center'> 

        <tr>    
            <td align='center'>Enter your email address</td>
            <td><input type="text" name="email" style="width: 200px;" /></td>
        </tr>

        </table>
        <input type='submit' name='submit' value='Post' />
    </form>

You need to execute mysql_fetch_row

if(isset($_POST['submit'])){

    $email = $_POST['email'];

    if($email==''){
    echo "<script>alert('Please enter email address!')</script>";
    exit();
    }

    $sql=mysql_query("select user_name from users where user_email='$email'");
     if(mysql_num_rows($sql)>=1)
       {
        $row = mysql_fetch_row($sql);
        //echo "<script>alert('Username $user_name already exist in our database, please try another one!')</script>";
        $username = $row[0];


        $subject = 'Forgotten Username';
        $message = 'Your username is: '.$username.'';
        $header = 'Header';

        if($_POST){    
        mail($email, $subject, $message, $header);
        $feedback = 'Thanks for contacting us! We will be in contact soon!';
            }
       }
     else {
     echo "<script>alert('Email doesn't exist')</script>";
     }
}
?>

To answer your question you need to grab the row before you can access it using mysql_fetch_row():

<?php
$row = mysql_fetch_row($sql);
$username = $row[0];

But it's more important to note that this is a very old method of accessing a mysql database.

See http://www.php.net/manual/en/function.mysql-fetch-array.php for the missing function, it'll also advice you about newer and safer methods of db access.

There is also a method mysql_real_escape_string() that you would've used to escape the email variable, but there are improved methods available.