从一个表中获取ID字段(字段名称相同)

时间:2013-09-21 05:14:38

标签: php mysql

我现在正试图从我的供应商表中获取ID字段。字段名称是ID。我以为我可以将'vendor.id'发送到我的sql查询,但它不断抛出错误而不返回任何数据。

我认为问题是我的用户表还有一个ID字段。是否有一种特殊的方式向函数发送请求而不是'vendor.id'

调用函数(最后是'vendor.id'

$data2 = display_all_orders($limit, 'fName', 'lName', 'VendorName', 'DateRequested', 'Shipping', 'VendorNumber', 'VendorFax', 'VendorAddress', 'VendorCity', 'VendorState', 'VendorZip', 'EquipmentConsumable', 'GasType', 'GasLocation', 'UNMTag', 'EquipmentLocation', 'index', 'totalcost', 'Approved', 'Shipped', 'user_id', 'vendor.id');

抓取并返回数据的函数:

function display_all_orders($limit)
{
    $data = array();
    $limit = (int)$limit;

        $func_num_args = func_num_args();
        $func_get_args = func_get_args();

//  print_r($func_get_args);
                if ($func_num_args > 1)
                        {
                                unset($func_get_args[0]);



                                $fields = '`' . implode('`, `', $func_get_args) . '`';


                $results = mysql_query("SELECT $fields  FROM  `users` ,  `vendor` WHERE users.id = vendor.user_id ORDER BY vendor.DateRequested DESC");
                for($x = 0; $x < $limit; $x++)
                {   

                $data[] = mysql_fetch_assoc($results);
                }
                return $data;
            }

}
?>

如果我没有尝试获得vendor.id它可以正常工作但是我发现我现在需要它......这些小东西太烦人了!

好消息是我终于可以看到光明了!谢谢大家在这里帮助我完成我的第一个webapp!

1 个答案:

答案 0 :(得分:2)

您的代码会生成包含`vendor.id`的请求,这是一个错误的字段名称。

使用:

$data2 = display_all_orders($limit, 'fName', 'lName', 'VendorName', 'DateRequested', 'Shipping',     
    'VendorNumber', 'VendorFax', 'VendorAddress', 'VendorCity', 'VendorState', 'VendorZip', 
    'EquipmentConsumable', 'GasType', 'GasLocation', 'UNMTag', 'EquipmentLocation', 'index', 'totalcost',  
    'Approved', 'Shipped', 'user_id', 'vendor`.`id');

应该有用,但这是一个黑客......

相关问题