Repeating PHP Queries with New Select Values?
Is there an easier way to do this instead of writing the same line of code 100+ times? I need the value of the field L_key each time as you will notice:
$query1 = "SELECT L_key FROM profiles WHERE 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 WHERE v_key = '$L2_key'";
mysqli_query($dbh, $query2);
}
$query3 = "SELECT L_key FROM profiles WHERE v_key = '$L2_key'";
$result = mysqli_query($dbh, $query1);
if ($row = mysqli_fetch_array($result))
{
$L3_key = $row['L_Key'];
开发者_StackOverflow $query2 = "UPDATE profiles SET min = min + 1 WHERE v_key = '$L3_key'";
mysqli_query($dbh, $query2);
}
$query3 = "SELECT L_key FROM profiles WHERE 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 WHERE v_key = '$L4_key'";
mysqli_query($dbh, $query2);
}
$query3 = "SELECT L_key FROM profiles WHERE 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 WHERE v_key = '$L5_key'";
mysqli_query($dbh, $query2);
}
Do I use a loop? If so, can you please show me a code to execute this over and over as I am still learning and do not know what a loop is? Or, is there a different method?
You have a recursive structure in your profiles table (v_key => (l_key : v_key)=> ( l_key... )
) and SQL does not really handle recusion terribly well with simple queries. Your options are to write a stored procedure or handle this with PHP. Since your a beginner, I'd imagine that PHP is far simpler for this task:
<?php
// define a variable on the outside -- we'll need it for each iteration
$lKey;
// If you know how many are going to be used, use a for loop because
// then you know you won't get some sort of nasty infinite recursion issue.
// $count is however many times this needs to operate.
for($i = 0; $i < $count; $i++ )
// while( true ) // <-- this will keep going until "break" is called.
// If you don't know how many will be used. Comment out the for loop
// and uncomment thie while loop.
{
// your initial query -- I added a limit because you only need one
// and there is no sense in doing anything more than what you need
$query1 = "SELECT L_key FROM profiles WHERE v_key = '$lKey' LIMIT 1";
$result = mysqli_query($dbh, $query1);
if ($row = mysqli_fetch_array($result)) // so far so good.
{
$lKey = $row['L_Key']; // assign that outside variable to the new key
// and run the necessary update.
$query2 = "UPDATE profiles SET min = min + 1 WHERE v_key = '$lKey'";
mysqli_query($dbh, $query2);
}
else
{
break;
}
// as of right now, $lKey is now the value from the select above.
// which means that you'll be able to start the next loop with it.
}
If I understand correctly, you can use the mysql_num_rows OR mysql_result to get the total numbers, so you can use a while:
<? $a = 0; while($total != $a) { //query $a++; } ?>
Use pdo and solve all your worries. This is cinche in pdo using prepared statements. Will even perform better.
精彩评论