Search code examples
phpmysqlmysql-error-1064multiple-tables

Trouble executing multiple-table mysql query from PHP


I am attempting to match rows in a mysql table using the values from table1.column1 and table2.column3 and then copy the value from table2.column2 into table1.column1 for each match. The query below does what I need to do, but only when I execute it manually (through phpmyadmin). When I try to execute it from PHP I receive the error Unknown column table1.column1 in 'field list'. Here is my PHP code:

<?php
mysql_connect($host,$user,$pass);
$db_selected = mysql_select_db($data);

$sql = "UPDATE table1 t1, table2 t2 
        SET t1.column1 = t2.column2 
        WHERE t1.column1 = t2.column3";

$result = mysql_query($sql);

if (!$result) {
  echo mysql_error();
} ?>

I know that the mysql connection info works because I am able to execute other queries. From my research on the error it seems that I might need backticks around some part of the query but after several tries I can't figure out the correct way.

EDIT 1 - As requested here is the real query:

UPDATE wp_mf_custom_groups,wp_mf_posttypes
SET wp_mf_custom_groups.post_type=wp_mf_posttypes.type
WHERE wp_mf_custom_groups.post_type=wp_mf_posttypes.id

Outputs the error

Unknown column 'wp_mf_custom_groups.post_type' in 'field list'

Additional information I just realized might be conflicting with it. Before this happens I also renamed the table using:

RENAME TABLE wp_mf_module_groups TO wp_mf_custom_groups

Maybe since the table was just renamed it cant reference it?


Solution

  • Worked when I added backticks to the columns only after WHERE

    UPDATE wp_mf_custom_groups,wp_mf_posttypes
    SET wp_mf_custom_groups.post_type=wp_mf_posttypes.type
    WHERE wp_mf_custom_groups.`post_type=wp_mf_posttypes.id