Author Topic: Check username  (Read 534 times)

0 Members and 1 Guest are viewing this topic.

Offline peter11Topic starter

  • Irregular
  • Posts: 42
    • View Profile
Check username
« on: March 07, 2010, 08:43:04 AM »
Hi all,

Im new here and new to PHP. I cant work out why the following is not working. I hope you can tell me how to fix and why.

$checklogin=("SELECT * FROM members");

if (
$checklogin==true){
	
echo 
"TAKEN";
}

else
echo 
"free";


Code: [Select]
<input name="$checklogin">
<input type="submit" title="submit">

What have i done wrong :S

thansk.

Offline jskywalker

  • Enthusiast
  • Posts: 289
  • Gender: Male
    • View Profile
Re: Check username
« Reply #1 on: March 07, 2010, 09:36:53 AM »
Why do you think the function mysql_query  does exist?

This function should be used to tell the database that you want to  "SELECT....." some things...

But before you can ask something from the database you should connect to it (use: mysql_connect)

Offline peter11Topic starter

  • Irregular
  • Posts: 42
    • View Profile
Re: Check username
« Reply #2 on: March 07, 2010, 03:23:44 PM »
I have connected the DB earlier in the code.

still cant get it to work with

$checklogin mysql_query($query);

$query = ("SELECT * FROM members");

if (
$query=="[username]"){
	
echo 
"TAKEN";
}

else
echo 
"free";


any help?

Offline prashanth

  • Irregular
  • Posts: 17
  • Gender: Male
    • View Profile
Re: Check username
« Reply #3 on: March 08, 2010, 06:48:49 AM »
Hi here is the code for your problem

$con = mysql_connect("hostname","mysql username","mysql password");
mysql_select_db("databse name",$con);

$query = "SELECT * FROM members";
$checklogin = mysql_query($query);
while($res = mysql_fetch_array($checklogin,MYSQL_ASSOC)) {
$result[] = $res;
}

the results will have the details

so now you can loop the results and you can check the conditions as your wish.

Let me know if you need in detail code i will provide.

Offline peter11Topic starter

  • Irregular
  • Posts: 42
    • View Profile
Re: Check username
« Reply #4 on: March 08, 2010, 08:20:33 AM »
I have no tried this yet however it would really help if you explaned some of it for me :) as i am new to php.

I dont understand the following:

You have a $result in there. Does php carry out the $'s even if they are not linked? As i can see $checklogin says its a query then carry out $query which asks the database for this info. Once it has this info there is nouthing to tell it what to do with this is there?

$result[] ??

also

$query "SELECT * FROM members"; Why are the brackets () not used around the select from members? Or is this not needed?

thanks for the help :) i hope this works.

Offline peter11Topic starter

  • Irregular
  • Posts: 42
    • View Profile
Re: Check username
« Reply #5 on: March 08, 2010, 01:16:30 PM »
I have tried what you said and its saying free every time. Its connecting fine. What have i got wrong here:

// Connect // 

$con mysql_connect("$host""$username""$password")or die("cannot connect");
mysql_select_db("$db_name"$con)or die("cannot select DB");

// CHECK LOGIN //

$query = ("SELECT * FROM members");
$checklogin mysql_query($query);

while(
$res mysql_fetch_array($checklogin,MYSQL_ASSOC)){
$result[] = $res;
}

if (
$checklogin==$res){
	
echo 
"TAKEN";
}
else
	
echo 
"FREE";

Offline jskywalker

  • Enthusiast
  • Posts: 289
  • Gender: Male
    • View Profile
Re: Check username
« Reply #6 on: March 08, 2010, 01:42:45 PM »
This code is too advanced for you

You better start here: http://www.w3schools.com/php/

and LEARN how to do it, in stead of asking ....

Offline prashanth

  • Irregular
  • Posts: 17
  • Gender: Male
    • View Profile
Re: Check username
« Reply #7 on: March 10, 2010, 07:22:35 AM »
Hello  peter11

by seing your post title it seems you are checking the username with database records

here is the logic
----------------------
// Connect //

$con = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name", $con)or die("cannot select DB");

// CHECK LOGIN //

$query = ("SELECT * FROM members WHERE username='".$_POST['username'] ."'");
$checklogin = mysql_query($query);
$res = mysql_fetch_array($checklogin,MYSQL_ASSOC);

if (!empty($res)){
echo "TAKEN";
}
else
echo "FREE";


Still if you are not clear let me know
« Last Edit: March 10, 2010, 07:23:25 AM by prashanth »

Offline peter11Topic starter

  • Irregular
  • Posts: 42
    • View Profile
Re: Check username
« Reply #8 on: March 10, 2010, 12:57:59 PM »
This works. I now see how it works. thanks :)