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. Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/ 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. Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/#findComment-295113 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() ? Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/#findComment-295116 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 } Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/#findComment-295125 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 } Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/#findComment-295128 Share on other sites More sharing options...
maxudaskin Posted July 11, 2007 Author Share Posted July 11, 2007 It is automatic. Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/#findComment-295138 Share on other sites More sharing options...
mmarif4u Posted July 11, 2007 Share Posted July 11, 2007 It is automatic.??? Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/#findComment-295142 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; Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/#findComment-295150 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? Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/#findComment-295151 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. Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/#findComment-295153 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. Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/#findComment-295155 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); Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/#findComment-295164 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. Link to comment https://forums.phpfreaks.com/topic/59404-how-can-i-php-and-mysql-question/#findComment-295175 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.