<?php
include 'Database.php';
//New user registration
function register_user($username, $password, $email){
//Check is the username already in the database
$check = mysql_query("SELECT * FROM users WHERE username = '$username'");
$result = mysql_num_rows($check);
//Check if the email is already in the database
$check2 = mysql_query("SELECT * FROM users WHERE email = '$email'");
$result2 = mysql_num_rows($check2);
//If username exists throw and error
if($result > 0){
$reg_error = "This username is already taken!";
include 'regform.php';
exit;
}
//If email exists throw an error
else if ($result2 > 0){
$reg_error = "This email is already taken!";
include 'regform.php';
exit;
}
//encrypt password with md5
$encrypted = md5($password);
//Register user
mysql_query("INSERT INTO users (username, password, email) VALUES ('$username', '$encrypted', '$email')");
}
?>
So as you can see i am developing a registration script. Everyting works great except that when i try to close mysql connection in functions.php i get an error "Undefined variable: link " I have included database.php in functions.php but still i cant use this variable. So what i am doing wrong? Also i've read somewhere that php automatically closes mysql connection when the script ends... is it true?
Unless you're using multiple databases which is doubtful and smells like bad practice, why do you need $link?
I don't know why it would be undefined other than saying that PHP is crazy and you may want to check for errors using error_reporting(E_ALL) at the top of your script.
From the PHP documentation of mysql_connect:
Note: The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close().
Don't you think that's the first place you should look?