maxudaskin Posted July 11, 2007 Share Posted July 11, 2007 How would I go around getting information from a database and comparing it to some page generated information... $hub = $_GET["hub"]; $rand = rand(001,999); $pid = $hub+$rand; I want it to compare $pid to information in a database and if that number is already being used, to redo the script. I know how to get it to re-do the script... I need to know how to compare to a db. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 11, 2007 Share Posted July 11, 2007 You need to run a SELECT query to pull the data from the specific table. Then it's a simple case if using if() to check the contents and act on the results. Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 11, 2007 Author Share Posted July 11, 2007 I mean... I don't know how to get the information to compare. $hostname_LeadHost = "***"; $database_LeadHost = "***"; $username_LeadHost = "***"; $password_LeadHost = "***"; $LeadHost = mysql_pconnect($hostname_LeadHost, $username_LeadHost, $password_LeadHost) or trigger_error(mysql_error(),E_USER_ERROR); $id = $_GET["apps"]; /* Ignore */ $hub = $_GET["hub"]; $huba = $hub/1000; /* Ignore */ function assignpid() { $rand = rand(001,999); $pid = $hub+$rand; $result = mysql_query("SELECT pid FROM `users` WHERE `pid`='{$pid}'"); } What would I put in the parentheses in if() ? Quote Link to comment Share on other sites More sharing options...
rameshfaj Posted July 11, 2007 Share Posted July 11, 2007 Do like this: $data_from_database is the row returned from the result after executing the query. if($data_from_database==$entered_data) { //do something } else { //do other thing } Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted July 11, 2007 Share Posted July 11, 2007 if (($result, 0) > 0) { echo "used. Please choose another."; } else { some code } Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 11, 2007 Author Share Posted July 11, 2007 It is automatic. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted July 11, 2007 Share Posted July 11, 2007 It is automatic.??? Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 11, 2007 Author Share Posted July 11, 2007 $rand = rand(001,999); $pid = $hub+$rand; Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 11, 2007 Author Share Posted July 11, 2007 How would I be able to make $pid unique in the event that it is already taken? Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted July 11, 2007 Share Posted July 11, 2007 did u try this code: if (($result, 0) > 0) { echo "used. Please choose another."; } else { some code } One thing why u r using rand. Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 11, 2007 Author Share Posted July 11, 2007 I am using rand() so that we do not have to input a pid (Pilot ID) each time. We want it to be random but unique. Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 11, 2007 Author Share Posted July 11, 2007 Is this good? Would it make sure $temppid is unique? $id = $_GET["apps"]; $hub = $_GET["hub"]; $huba = $hub/1000; $rand = rand(001,999); $temppid = $hub+$rand; $result = mysql_query("SELECT pid FROM `users` WHERE `pid`='{$temppid}'"); if (mysql_num_rows($result) == 1) { $pidcheck = 1; } else { $pidcheck = 0; } do { $$temppid++; } while ($pidcheck==1); Quote Link to comment Share on other sites More sharing options...
mosi Posted July 11, 2007 Share Posted July 11, 2007 How about set a column in the users table called pid, set it to int and set it to auto increment? That would keep it unique and auto generate it for you each time you add a new row to the table. Quote Link to comment 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.