PHPNewbie55 Posted March 14, 2009 Share Posted March 14, 2009 I am trying to match up two fields in two separate tables in a mysql database. The fields contain words and spaces... Example: This is the content My first question is... What would be the best way to query the two tables?? Should I run a "while" inside a "while"...? $p_se = mysql_query("SELECT * FROM table1"); $t_se = mysql_query("SELECT * FROM table2"); while($pse = mysql_fetch_array($p_se)) { while($tse = mysql_fetch_array($_se)) { $p_name = "".addslashes($pse['name']).""; $t_name = "".addslashes($tse['name']).""; // this is where I want to match the data from the two table queries..... $pos = strpos($t_name, $p_name); } } Any example of a way to query the two tables more efficiently.. would be greatly appreciated.. My second question is... how would I match up the data..?? I have intentionally inserted several duplicate records to test the matching process and I cannot get the fields to match up.. any examples of things that I could try would be great... Thanks!! Link to comment https://forums.phpfreaks.com/topic/149366-solved-help-with-matching-two-fields-preg_match-strpos/ Share on other sites More sharing options...
lonewolf217 Posted March 14, 2009 Share Posted March 14, 2009 have you tried using strcasecmp() ? Link to comment https://forums.phpfreaks.com/topic/149366-solved-help-with-matching-two-fields-preg_match-strpos/#findComment-784480 Share on other sites More sharing options...
PHPNewbie55 Posted March 14, 2009 Author Share Posted March 14, 2009 I have tried that (since you suggested it) BUT --- What would the result of this be... a TRUE or FALSE..??? Or is this the proper way to use strcasecmp() $pos = strcasecmp($t_name, $p_name); Remember I am a Newb... LOL The next line is.... if($pos == "true") { mysql_query("UPDATE temp SET ismatch='yes', msku='".$pse['pid']."' WHERE sku='".$tse['pid']."'"); } Link to comment https://forums.phpfreaks.com/topic/149366-solved-help-with-matching-two-fields-preg_match-strpos/#findComment-784819 Share on other sites More sharing options...
PHPNewbie55 Posted March 14, 2009 Author Share Posted March 14, 2009 I looked up strcasecmp() and started using it correctly... but it doesn't work.. I can't get any items to match even though I have a bunch of identical items... So I guess matching up two fields in a table is impossible to do... I have tried everything that I could find and I cannot get a match within a table that definitely has matches contained within the info... The only option I have now is "TOPIC SOLVED"... which wouldn't be true... Oh well I'll keep looking and if I resolve this issue I will come back and mark this "TOPIC SOLVED".... My latest attempt:::: $pos = strcasecmp("".$t_name."", "".$p_name.""); if($pos == 0) { mysql_query("UPDATE temp SET ismatch='yes', msku='".$pse['pid']."' WHERE sku='".$tse['pid']."'"); } Link to comment https://forums.phpfreaks.com/topic/149366-solved-help-with-matching-two-fields-preg_match-strpos/#findComment-784832 Share on other sites More sharing options...
Rodis Posted March 15, 2009 Share Posted March 15, 2009 The best way to query two tables is using JOIN this way you only have one query accessin the two tables. http://www.tizag.com/mysqlTutorial/mysqljoins.php or you could use left join http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php Just see for yourself Link to comment https://forums.phpfreaks.com/topic/149366-solved-help-with-matching-two-fields-preg_match-strpos/#findComment-784937 Share on other sites More sharing options...
Rodis Posted March 15, 2009 Share Posted March 15, 2009 Also i don't know why u want to match the results but if you want to get some fields where the 2 matches as a result you could use inner join like so $select = "SELECT * FROM table1 INNER JOIN table2 ON table1.name = table2.name WHERE name = 'john' AND age = '12'"; $query = mysql_query($select); while ($row = mysql_fetch_array($query)){ $name = "The name is: ".addslashes($row['name']).""; } Link to comment https://forums.phpfreaks.com/topic/149366-solved-help-with-matching-two-fields-preg_match-strpos/#findComment-784946 Share on other sites More sharing options...
PHPNewbie55 Posted March 15, 2009 Author Share Posted March 15, 2009 Rodis... I want to match the fields so I can compare the two items... Example.. if I have two items that are identically named but have different specs ( such as Large and Small ) I want to be able to view those two items at once... Thanks for the help and I will try that ASAP and let you know what I come up with.. Link to comment https://forums.phpfreaks.com/topic/149366-solved-help-with-matching-two-fields-preg_match-strpos/#findComment-784977 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.