从表名数组中提取列值

时间:2009-06-16 18:01:53

标签: php mysql

下面的代码在名为“site”的列中打印出名为“feather”的数据库中包含“$ entry”的所有表。 “feather”中的每个表都有一个名为“site”的列。

此代码效果很好。但是,我想补充一点。 “feather”中的每个表都包含一个名为“votes_up”的列。对于每个包含“$ entry”的表,我想打印出与$ entry对应的“votes_up”列的值。我该怎么做呢?

提前致谢,

约翰

$result = mysql_query("SHOW TABLES FROM feather") 
or die(mysql_error()); 

while(list($table)= mysql_fetch_row($result))
{
  $sqlA = "SELECT COUNT(*) FROM `$table` WHERE `site` LIKE '$entry'";
  $resA = mysql_query($sqlA) or die("$sqlA:".mysql_error());
  list($isThere) = mysql_fetch_row($resA);
  if ($isThere)
  {
     $table_list[] = $table;
  }
}



foreach( $table_list as $key => $value){
    echo "$value <br />";
}

2 个答案:

答案 0 :(得分:1)

怎么样:

SELECT COUNT(*), sum(votes_up) FROM `$table` WHERE `site` LIKE '$entry'

这将加起来所有的upvotes,并计算所有行。

答案 1 :(得分:1)

这假设您希望表格中每次出现'entry'都需要'投票'输出。

while(list($table)= mysql_fetch_row($result))
{
 $sqlA = "SELECT `site`,votes_up FROM `$table` WHERE `site` LIKE '$entry'";
 $resA = mysql_query($sqlA) or die("$sqlA:".mysql_error());
 if(mysql_num_rows($resA) > 0)
 {
$table_list[] = $table;
while($rowA = mysql_fetch_assoc($resA))
  {
  $votes_up[$rowA["site"]] = $rowA["votes_up"];
  }
 }
}

foreach( $table_list as $key => $value){
    echo "$value <br />";
}

foreach($votes_up as $site => $vote_up)
{
  echo "$site: $vote_up<br />";
}