loops - Repeating PHP Queries with New Select Values? -


is there easier way instead of writing same line of code 100+ times? need value of field l_key each time notice:

$query1 = "select l_key profiles v_key = '$l1_key'"; $result = mysqli_query($dbh, $query1); if ($row = mysqli_fetch_array($result))  {     $l2_key = $row['l_key'];     $query2 = "update profiles set min = min + 1 v_key = '$l2_key'";     mysqli_query($dbh, $query2); }   $query3 = "select l_key profiles v_key = '$l2_key'"; $result = mysqli_query($dbh, $query1); if ($row = mysqli_fetch_array($result))  {     $l3_key = $row['l_key'];     $query2 = "update profiles set min = min + 1 v_key = '$l3_key'";     mysqli_query($dbh, $query2); }   $query3 = "select l_key profiles v_key = '$l3_key'"; $result = mysqli_query($dbh, $query1); if ($row = mysqli_fetch_array($result))  {     $l4_key = $row['l_key'];     $query2 = "update profiles set min = min + 1 v_key = '$l4_key'";     mysqli_query($dbh, $query2); }   $query3 = "select l_key profiles v_key = '$l4_key'"; $result = mysqli_query($dbh, $query1); if ($row = mysqli_fetch_array($result))  {     $l5_key = $row['l_key'];     $query2 = "update profiles set min = min + 1 v_key = '$l5_key'";     mysqli_query($dbh, $query2); }  

do use loop? if so, can please show me code execute on , on still learning , not know loop is? or, there different method?

you have recursive structure in profiles table (v_key => (l_key : v_key)=> ( l_key... )) , sql not handle recusion terribly simple queries. options write stored procedure or handle php. since beginner, i'd imagine php far simpler task:

<?php // define variable on outside -- we'll need each iteration $lkey; // if know how many going used, use loop because  // know won't sort of nasty infinite recursion issue. // $count many times needs operate. for($i = 0; $i < $count; $i++ ) // while( true ) // <-- keep going until "break" called. // if don't know how many used. comment out loop // , uncomment thie while loop. {     // initial query -- added limit because need 1     // , there no sense in doing more need     $query1 = "select l_key profiles v_key = '$lkey' limit 1";     $result = mysqli_query($dbh, $query1);     if ($row = mysqli_fetch_array($result)) // far good.      {         $lkey = $row['l_key']; // assign outside variable new key         // , run necessary update.         $query2 = "update profiles set min = min + 1 v_key = '$lkey'";         mysqli_query($dbh, $query2);     }      else     {         break;     }     // of right now, $lkey value select above.     // means you'll able start next loop it. } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -