PHP help

Questions about programming languages and debugging
Post Reply
User avatar
Macross
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 152
Joined: 01 May 2007, 16:00
16
Contact:

PHP help

Post by Macross »

<b>Database.php</b>

Code: Select all

<?php

$host = 'localhost';
$db_user = '****';
$db_pass = '*****';
$db_selected = 'groups';

$link = mysql_connect($host, $db_user, $db_pass);
mysql_select_db($db_selected);

?>
<b>Functions.php</b>

Code: Select all

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

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

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?

Also SQL injections

User avatar
Macross
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 152
Joined: 01 May 2007, 16:00
16
Contact:

Post by Macross »

Thanks for info. SQL injection is taken care of in a different file using mysql_real_escape_string and stripslashes so it's not a problem.

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Post by Gogeta70 »

And to answer your question anyway, to use the $link variable inside of a function, you have to do the following:

Code: Select all

function whatever(arg1, arg2)
{ GLOBAL $link, $var, $var;

//some stuff here

}
But yes, leet's right, php closes the connection automatically.
¯\_(ツ)_/¯ It works on my machine...

Post Reply