PHP - anyone know how to put this in a while loop

Discussion in 'PHP' started by rickyj, Jul 7, 2007.

  1. #1
    anyone got an idea how to complete the following while loop. I want to set a database column (LT_URL) to a sting ($ltURLarray),
    but 1st i want to check if it already exist in the db, if it does exist, then I want to test the next sting exist in the db, if it doesnt set LT_URL to $ltURLarray on the i'th attempt.
     
    
    $ltURLarray[0] = 'string0';
    $ltURLarray[1] = 'string1'; //.......
    $ltURLarray[100] = 'string100';
    
    // some how put the follwing in a while loop while i<100:
    i = 0;
    URLdata = mysql_query("SELECT LT_URL FROM lt_Users WHERE LT_URL = '$ltURLarray[i]' LIMIT 1")or die(mysql_error()); 
    if (!mysql_num_rows($URLdata)){
    	$ltURL =$ltURLa[0];
    	} 
    Code (markup):

     
    rickyj, Jul 7, 2007 IP
  2. TheBorg

    TheBorg Peon

    Messages:
    144
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Never put MySQL queries in a loop. Better explain what you want to accomplish, there must be a better solution.
     
    TheBorg, Jul 7, 2007 IP
  3. TheBorg

    TheBorg Peon

    Messages:
    144
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    However it would look like this.

    <?php
    
    	for ($i = 0; $i < 100; $i++)
    	{
    		$URLdata = mysql_query("SELECT LT_URL FROM lt_Users WHERE LT_URL = '".$ltURLarray[$i]."' LIMIT 1") or die(mysql_error()); 
    		if (mysql_num_rows($URLdata) === 0)
    		{
    			$ltURL =$ltURLa[$i];
    			mysql_query("INSERT INTO lt_Users (LT_URL) VALUES('".$ltURL."')");
    		}
    	}
    
    ?>
    PHP:
    Not tested, debugged or checked for typos. But it should work.
     
    TheBorg, Jul 7, 2007 IP