Jump to content

[SOLVED] Help with matching two fields.. preg_match? strpos?


PHPNewbie55

Recommended Posts

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!!

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']."'");	
}

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']."'");
}

 

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

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'])."";
}

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..

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.