/    Sign up×
Community /Pin to ProfileBookmark

Function () does not work – help please

I have the following function () and it does not give answer:
“This <b>Username</b> is not valid” ;
Do you know why it does not shows any response in window?
Is possible that function gives only true or false and not echo, I hope you can help. My code for function.
function validation_search ()
{
if (isset($_POST[‘fname’]))
{
if ($un!=””)
{ $query = “SELECT usernames FROM users WHERE username=’$un'”;
$result = mysql_query($query) or die (“SQL error: $query — ” . mysql_error());
$num_rows = mysql_num_rows($result); //Check how many rows are in result

if($num_rows > 0) {}
else {echo “This <b>Username</b> is not valid” ;
return false;}
}
else {}
}
else
{}

}

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@ScleppelSep 23.2005 — it's never going to return anything because $un doesn't exist in the functions scope ($_POST and other preset arrays do exist because they are superglobal). you need to either pass the variable to the function like this
[code=php]
//replace this line
function validation_search()
//to this
function validation_search($un)
//and call using
validation_search($some_variable);
//where $some_variable is the value of $un in the function
[/code]

or, if you're trying to get a value already set in the script, you can bring it into the functions scope by making it global
[code=php]
//replace
function validation_search()
{
//with
function validation_search()
{
global $un;
[/code]


More about variable scope: http://php.net/variables.scope
Copy linkTweet thisAlerts:
@toplisekauthorSep 24.2005 — thanks for fast reply. I'm new to this.

Please explain me this: $un is variable that I have value when form is posted.

e.g. username value: john. when this username in form is not the same as in the Database, it gives function validation_search FALSE, else is TRUE.

What do I have to change? Please suggest...Error is the following:

Fatal error: Call to undefined function: validation_search()

I have in other file:

if (validation_search ($search))

{

...

}

and function:

$un = $_POST['username'];

function validation_search ($search)

{ global $un;

if (isset($_
POST['submit']))

{

if ($un!="")

{ $query = "SELECT usernames FROM users WHERE username='$un'";

$result = mysql_query($query) or die ("SQL error: $query -- " . mysql_error());

$num_rows = mysql_num_rows($result); //Check how many rows are in result

if($num_rows > 0) {}
else {echo "This <b>Username</b> is not valid";return false;}
}
else { return true;}

}

else {return true;}



}
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYSep 24.2005 — well, you have to define the function before calling it
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYSep 24.2005 — i am working on cleaning your code,

so basically what you want someone that enters a username in a form to check its availability?

what is $_POST['submit']?

what do you want it to output?
Copy linkTweet thisAlerts:
@toplisekauthorSep 24.2005 — when form is SUBMITTED it should check the form content (if (isset($_POST['fname'])) )

1. Field Username will be checked if there is input in field Username

(if ($un!=""))

2. When field username is filled it will check availability.

( { $query = "SELECT usernames FROM users WHERE username='$un'"?

If there is Username value not the same as in Database it will be function false and returnes message Username is not valid

( else {echo "This <b>Username</b> is not valid";return false;} )

if user is in Database it will return TRUE

i hope you can help
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYSep 24.2005 — yes, give me a minute
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYSep 24.2005 — i didnt test it
[code=php]
<?php
$username = $_POST['username'];
#path to database information
$db_connect = "../db/db_config.php";

#function with 2 parametres
# $username is the username from the field
# $db_connect is the path to the file that contains the
# informations you need to connect to the database

function validation_search($username, $db_connect){

#include that file
require_once($db_connect);
#connect to database
$connection = mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
#select database (database name should also be defined in the config file)
mysql_select_db($db_name);
#query and num_rows
$query = "SELECT usernames FROM users WHERE username='$un'";
$result = mysql_query($query, $connection) or die(mysql_errror());
$num_rows = mysql_num_rows($result);

if($num_rows){
return true;
}else{
return false;
}

}
if(!empty($username)){
if(validation_search($username, $db_connect)){
echo "This username [$username] is not valid";
}else{
echo "This username [$username] is valid";
}
}
?>
[/code]
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYSep 24.2005 — and tell me if there is something you don't understand.

i might have done this script too specific, but in the end, i think it's what you'll need
Copy linkTweet thisAlerts:
@toplisekauthorSep 24.2005 — Function still shows all the time message:

I found [...] in Database even i put new user in form.

there is as would be all the time FALSE in function.What do you think?

Code:

$un = $_POST['username'];

if (isset($_POST['SUBMIT']))

{

function validation_search ($username, $db)

{



$query = "SELECT usernames FROM login WHERE usernames='$un' LIMIT 1";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result); //Check how many rows are in result
if($num_rows > 0){return true;} else {return false;}

}

if(!empty($un))

{

if(validation_search($username, $db))

{echo "This <b>Username</b> [$username] is not valid";}

else {

echo "I found [$username] in Database";

echo validation_search($username, $db);

}

}


}
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYSep 24.2005 — does the query work anyway?
Copy linkTweet thisAlerts:
@toplisekauthorSep 24.2005 — when I put in other file it works,

but when is in validation_search file there is not response. it is odd that it does not show me $ query. Why is than all the time FALSE?
Copy linkTweet thisAlerts:
@toplisekauthorSep 29.2005 — I have code and would like to check username and it is all the time FALSE:
[code=php]
<?php
//search for name
/* Connect to MySQL-Server */
...

/* Open $db */
...

$un = $_POST['user'];

function validation_search ($username, $db)
{



$query = "SELECT user FROM users WHERE user='$un' LIMIT 1";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result); //Check how many rows are in result
if($num_rows > 0){return true;} else {return false;}
}
if(!empty($un))
{
if (validation_search($username, $db))
{echo "This <b>Username</b> [$un] is valid";}
else
{echo "This <b>Username</b> [$un] is not valid";}

}


?>
[/code]
×

Success!

Help @toplisek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...