Php Mysql如果列不存在,请选择另一个表

Php Mysql如果列不存在,请选择另一个表,php,mysql,Php,Mysql,我想从数据库中创建XML。数据保存在不同的表中。这就是为什么如果表中不存在列,我想从不同的表中选择列 如何编写Select来检查表是否具有所需的列 $sql = "SELECT products_quantity FROM ".$config['table_name'], $config['table_name2'].products_description; 这是完整的代码 <?php //database configuration $config['mysql_host'] = "

我想从数据库中创建XML。数据保存在不同的表中。这就是为什么如果表中不存在列,我想从不同的表中选择列

如何编写Select来检查表是否具有所需的列

$sql = "SELECT products_quantity FROM ".$config['table_name'], $config['table_name2'].products_description;
这是完整的代码

<?php
//database configuration
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "root";
$config['mysql_pass'] = "root";
$config['db_name']    = "sqltoxml";
$config['table_name'] = "products";
$config['table_name2'] = "products_description";


//connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");

$xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = $config['table_name']."s";
$xml         .= "<$root_element>";

//select all items in table
$sql = "SELECT products_id FROM ".$config['table_name'], products_description FROM ".$config['table_name2'];


$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

if(mysql_num_rows($result)>0)
{
   while($result_array = mysql_fetch_assoc($result))
   {
      $xml .= "<".$config['table_name'].">";

      //loop through each key,value pair in row
      foreach($result_array as $key => $value)
      {
         //$key holds the table column name
         $xml .= "<$key>";

         //embed the SQL data in a CDATA element to avoid XML entity issues
         $xml .= "<![CDATA[$value]]>";

         //and close the element
         $xml .= "</$key>";
      }

      $xml.="</".$config['table_name'].">";
   }
}

    //close the root element
$xml .= "</$root_element>";

//send the xml header to the browser
header ("Content-Type:text/xml");

//output the XML data
echo $xml;

$fn=  "export.xml";
$fp = fopen($fn,"wb");
$write = fwrite($fp,$xml);
fclose($fp);
?>
”;
}
$xml.=“”;
}
}
//关闭根元素
$xml.=“”;
//将xml标头发送到浏览器
标题(“内容类型:text/xml”);
//输出XML数据
echo$xml;
$fn=“export.xml”;
$fp=fopen($fn,“wb”);
$write=fwrite($fp,$xml);
fclose($fp);
?>

这两个表的架构是什么?如果表2引用了表1中的产品ID,则可以连接这两个表。使用If指定返回一个字段(如果该字段包含值),或者返回另一个字段

SELECT IF(table1.product_quantity,table1.product_quantity,table2.product_description)
FROM table1
JOIN table2 ON table1.id = table2.prodct_id;
这是关系数据库允许用户从多个表中获取数据的一种方式。除非提供了其他匹配条件,否则(在一列上)必须存在某种匹配,以表明您希望从与第一个表上的行相关的另一个表中调用行