If Row Exist, Continue Loop

Discussion in 'PHP' started by MesCasinos, Aug 28, 2010.

  1. #1
    Hi, i made this, i think this is working.. but i need the a final answer by an expert!

    Right now the script is working 100% with the multi-uploader. It seems to be working correct but im not sure when my databse will have 250 000 rows if its still gonna work fine.

    The goal is to add a unique $mid into the databse for the uploaded file.

    If the row exist, continue to make a random number with max 5 numbers until the $mid is unique in the database

    ;) The code is below, i pasted only the necessary code.

    	
    [...]
    move_uploaded_file($tempFile,$targetFile);
    		
    		//créé un mid de 5 chiffre unique
    		do{ //faire ceci en loop
    			$mid = rand(11111, 99999);
    			$result_mid = mysql_query("SELECT mid FROM wallpapers WHERE mid = '".$mid."'");
    			$numrows = mysql_num_rows($result_mid);
    			
    			if ($numrows == 0){
    				$uid_string = $sql_categorie.$sql_file;
    				$uid = hash(md5, $uid_string); // chaque marque a son md5.
    				mysql_query ( "INSERT INTO wallpapers VALUES ('', '$mid', '$uid', 'oui', '". $mature ."', '". $sql_categorie ."', '" .$sql_path. "', '" .$sql_file. "', '0');") or die (mysql_error());
    			}
    		}
    		while ($numrows); //exécute le do en loop avec une condition
    
    PHP:
    Thanks very much!
     
    MesCasinos, Aug 28, 2010 IP
  2. HungryMindz

    HungryMindz Member

    Messages:
    7
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #2
    Hi!

    First Of All...
    Don't Use Single Quotes With Numeric Value In Your Queries.
    U Set Numeric Value In $mid;
    So Don't With Single Quotes With $mid.
    $result_mid = mysql_query("SELECT mid FROM wallpapers WHERE mid = ".$mid);
    Do Same Action With This All Queries.

    Now! Wait For An Expert To Solve Your Problem :)
     
    HungryMindz, Aug 28, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    $mid = substr(uniqid(md5(microtime()), true), 0, 5); //5 chars
    PHP:
    That will be unique.
     
    danx10, Aug 28, 2010 IP
  4. MesCasinos

    MesCasinos Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah thanks but i need only digits, as exemple, look here and click on Web to Mobile orange button at your right (under the Preview).

    The mid code is for the WAP id to get the wallpaper on your mobile.

    I want it 5 digits.

    Plus your code verify nothing.
     
    MesCasinos, Aug 28, 2010 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    Let MySQL do the work for you. Make mid your primary key as auto increment with UNSIGNED ZEROFILL attribute of type int(5). This will give you numbers of 00000 until 99999.

    The problem being is after 99999 you either have to increase the digit count or delete ones that are used (if that is how it works).

    
    move_uploaded_file($tempFile,$targetFile);
    $uid_string = $sql_categorie.$sql_file;
    $uid = hash(md5, $uid_string); // chaque marque a son md5.
    // Assuming mid if the primary key, auto increment
    mysql_query ( "INSERT INTO wallpapers VALUES ( '$uid', 'oui', '". $mature ."', '". $sql_categorie ."', '" .$sql_path. "', '" .$sql_file. "', '0');") or die (mysql_error());
    // Get the unique mid generated by MySQL
    $mid = mysql_insert_id();
    
    PHP:
     
    ThePHPMaster, Aug 28, 2010 IP
  6. MesCasinos

    MesCasinos Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok great but normaly.. i already have ID auto increment in my table. this is my default. I still set mid as primary key ?

    JHere is a sc of my table structure

    structure
    [​IMG]

    Normal
    [​IMG]
     
    MesCasinos, Aug 30, 2010 IP
  7. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #7
    You can not have two auto increments or primaries at the same time.

    You have to remove the id field, however, you will have to update your coding to reflect it so and use the mid as the ID instead.
     
    ThePHPMaster, Aug 30, 2010 IP
  8. MesCasinos

    MesCasinos Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ok great and thanks imma use this right now. I already think about that method but for my personal knowledge I wish to know how to solve my problem
     
    MesCasinos, Aug 31, 2010 IP