Reputation: 169
This problem has been worrying me since 2 weeks.
Code :-
<?php
session_start();
include '../dbconnector.php';
function getRandUsername()
{
include '../dbconnector.php';
$u = rand(0,99999);
//check if the number exists in the database already
$query="SELECT * from admin where username = '" . $u . "'";
$result=mysql_query($query,$db) or die (mysql_error($db));
if(mysql_num_rows($result) > 0)
{
echo "Username Exists";
getRandUsername();
exit();
}
else
{
echo "Username Does not exist";
}
}
getRandUsername();
?>
Problem :- The problem is, that I need to include this dbconnector.php file twice. If I don't include it within the function parentheses, it would throw an error. While if the file is include in the function block, everything works fine.
What's wrong in here?
Thanks.
Upvotes: 0
Views: 23
Reputation: 80639
Try the following:
require_once '../dbconnector.php';
function getRandUsername() {
global $db; // where I'm assuming that $db is defined in the external file
Upvotes: 1