Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6081025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:07:42+00:00 2026-05-23T11:07:42+00:00

Is there an easier way to do this instead of writing the same line

  • 0

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'];
    $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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:07:43+00:00Added an answer on May 23, 2026 at 11:07 am

    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.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically need to check if there is an easier way to do this
There must be an easier way or function to do this code here: #!/usr/bin/env
Is there an easier way to write this? I know if i didnt need
Is there an easier way to do this (retaining the ability to use VIEW_VISIBLE,
I'm pretty sure there is an easier way to do this that I'm missing
There must be an easier way to do this. I have search and search
Is there an easier way to prevent a duplicate insert after refresh? The way
Is there an easier way of cleaning up a database that has a ton
IS there an easier way to animate the table view. All the rows in
Is there an easier way to get the path to an iOS application, than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.