peter11 Posted March 7, 2010 Share Posted March 7, 2010 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"; <input name="$checklogin"> <input type="submit" title="submit"> What have i done wrong :S thansk. Quote Link to comment https://forums.phpfreaks.com/topic/194415-check-username/ Share on other sites More sharing options...
jskywalker Posted March 7, 2010 Share Posted March 7, 2010 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) Quote Link to comment https://forums.phpfreaks.com/topic/194415-check-username/#findComment-1022669 Share on other sites More sharing options...
peter11 Posted March 7, 2010 Author Share Posted March 7, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/194415-check-username/#findComment-1022754 Share on other sites More sharing options...
prashanth Posted March 8, 2010 Share Posted March 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194415-check-username/#findComment-1022956 Share on other sites More sharing options...
peter11 Posted March 8, 2010 Author Share Posted March 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194415-check-username/#findComment-1022984 Share on other sites More sharing options...
peter11 Posted March 8, 2010 Author Share Posted March 8, 2010 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"; Quote Link to comment https://forums.phpfreaks.com/topic/194415-check-username/#findComment-1023117 Share on other sites More sharing options...
jskywalker Posted March 8, 2010 Share Posted March 8, 2010 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 .... Quote Link to comment https://forums.phpfreaks.com/topic/194415-check-username/#findComment-1023125 Share on other sites More sharing options...
prashanth Posted March 10, 2010 Share Posted March 10, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/194415-check-username/#findComment-1024198 Share on other sites More sharing options...
peter11 Posted March 10, 2010 Author Share Posted March 10, 2010 This works. I now see how it works. thanks Quote Link to comment https://forums.phpfreaks.com/topic/194415-check-username/#findComment-1024330 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.