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 3987668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:09:39+00:00 2026-05-20T06:09:39+00:00

disclaimer: I posted this on another site first I have a table (res_table) that

  • 0

disclaimer: I posted this on another site first

I have a table (res_table) that is about 200 columns wide. One of these columns is named “feature_lk”, and it consists of a string of numbers which are “|” delimited. The numbers stand for feature catagories which reside in another table named “features”

Thanks to this thread: http://www.kirupa.com/forum/showthread.php?t=224203 I figured out how to parse the features out!

Now my problem is how to look them up? I feel like I either need to join my two tables, but I’m not sure how, or I need to do a another select query for each of the features that I parse.. This is what I have to far (removed connection strings for posting purposes)

PHP Code:
<?php 
$sql = ("SELECT * FROM res_table"); 
$result = mysql_query($sql); 

while($row = mysql_fetch_array($result)) 
{ 
    $feature_string = $row['features_lk']; 
    $features = explode( '|', $feature_string ); 

    foreach( $features as $feature ) { 
        $feature = trim( $feature ); 
        echo $feature.': '; 

        $sql2 = "SELECT * from features where features.feature_id like $feature"; 
        $result2 = mysql_query($sql2); 
        while ($row2 = mysql_fetch_array($result2)) 
        { 
            $feat_desc = $row2['feature_description']; //this is another column in the features table 
            echo $feat_desc . '<br>'; 
        } 
    } 
    echo '<br>'; 
} 
?>

SO that works OK because when I run it, i’ll get about results that look like this:

13: None
62: Water Softener - Rented
71: Full
168: Barn
222: Storage Shed
226: Walkout
309: Detached
347: 2 Story
384: Attic Storage
439: Laundry Hook Up
466: Rural
476: Trees
512: School Bus
562: Mud Room
563: Pantry
2273: Septic Tank
643: Private Well

My question is: is there a better way to do this? There are about 10k rows in the main res_table with only a couple hundred hits, you can see that the number of select statements performed grows LARGE in no time at all.

I’m sure this is PHP + MySQL 101 stuff, but I’m just a beginner so any ideas? Thanks in advance.

  • 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-20T06:09:39+00:00Added an answer on May 20, 2026 at 6:09 am

    When you are storing more than one piece of information in a column, your table is not normalized. Doing lookups on feature_lk will necessarily be slow and difficult. feature_lk should become its own table:

    Table feature_lk:

    • res_table_id FK to res_table
    • feature_id FK to feature table
    • primary key(res_table_id,feature_id)

    Then your query is:

    SELECT f.* from features f 
      JOIN feature_lk lk ON (f.id=lk.feature_id) 
      JOIN res_table r ON (lk.res_table_id=r.id);
    

    One query only. No loop. No parsing out the features.

    ETA

    stored procedure for splitting an arbitrary length string by an arbitrary character

    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS `dorepeat` $$
    CREATE PROCEDURE `dorepeat`(in ToBeSplit LONGTEXT , in Splitter CHAR)
    Begin
    
    DECLARE TotalLength INT;
    DECLARE SplitterPosition INT;
    DECLARE SubstringLength INT;
    DECLARE SubstringStart INT;
    
    DROP Table if exists Split_Values;
    CREATE temporary TABLE Split_Values (split varchar(255));
    
    SET TotalLength = LENGTH(ToBeSplit);
    SET SplitterPosition = LOCATE(Splitter, ToBeSplit);
    SET SubstringStart = 1;
    
    ss: WHILE SplitterPosition < TotalLength DO
            IF SplitterPosition!=0 THEN 
                    SET SubstringLength = SplitterPosition - SubstringStart;
                    Insert into Split_Values VALUES (SUBSTRING(ToBeSplit,SubstringStart,SubstringLength));
                    SET SubstringStart = SplitterPosition+1;
                    SET SplitterPosition = LOCATE(Splitter, ToBeSplit, SplitterPosition+1);
            ELSE
                    Insert into Split_Values VALUES (SUBSTRING(ToBeSplit,SubstringStart));
                    SET SplitterPosition=TotalLength;
            END IF;
    END WHILE ss;
    End $$
    
    DELIMITER ;
    

    Using dorepeat in another procedure makes temp table with res_table_id and each feature:

    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS `multido` $$
    CREATE PROCEDURE `multido`()
    Begin
    DECLARE done INT default 0;
    DECLARE rt_id INT (10);
    DECLARE features LONGTEXT;
    DECLARE mycur cursor for select distinct res_table_id, feature_lk from res_table WHERE feature_lk!='';
    DECLARE continue handler for sqlstate '02000' set done=1;
    drop table if exists tmpfeatures;
    create temporary table tmpfeatures( res_table_id int(10),  feature varchar(255));
    open mycur;
    repeat
      fetch mycur into rt_id,features;
      call dorepeat(features,'|');
      insert into tmpfeatures select rt_id, trim(split) from Split_Values;
    until done end repeat;
    close mycur;
    
    End $$
    
    DELIMITER ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: I have attempted to contact the developer about this plugin, but silence reigns.
Disclaimer Yes, I am fully aware that what I am asking about is totally
Disclaimer: This is my first time writing unit tests...be gentle! :) I am trying
Disclaimer: JS novice I have a JS widget that depends on JQuery. The widget's
Disclaimer, I'm not a PHP programmer, so you might find this question trivial. That's
Disclaimer When I wrote this question I was wrong about the behavior of the
Disclaimer : this is a strange issue that only happens in a Kindle Fire
Disclaimer: I would love to be using dependency injection on this project and have
Disclaimer: This question is not about fixing visual studio So, I've used VSS for
( Disclaimer: This question is not specific to ASP.NET) I have a control which

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.