Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL,PHP:使用值以逗号分隔的另一个表输出表值_Php_Mysql - Fatal编程技术网

SQL,PHP:使用值以逗号分隔的另一个表输出表值

SQL,PHP:使用值以逗号分隔的另一个表输出表值,php,mysql,Php,Mysql,我有两个表,第一个是包含出版物的researchprojects,这些是逗号分隔的数字,另一个是publi,我有Pub_id,其中相同的值不以逗号分隔,还有Pub_year等列。我的问题是,我如何链接这两个表并输出Pub_id,Pub_year值 研究项目表 publi表 创建一个数组,该数组将以键和值对的形式在数组中存储发布id和发布年份 <?php $pub = [800 => 2015, 900 => 2016, 1000 => 2017]; foreach (

我有两个表,第一个是包含出版物的researchprojects,这些是逗号分隔的数字,另一个是publi,我有Pub_id,其中相同的值不以逗号分隔,还有Pub_year等列。我的问题是,我如何链接这两个表并输出Pub_id,Pub_year值

研究项目表

publi表


创建一个数组,该数组将以键和值对的形式在数组中存储发布id和发布年份

<?php 

$pub = [800 => 2015, 900 => 2016, 1000 => 2017];
foreach ($pub as $pubId => $pubYear) 
{
    echo "<td>{$pubId}, {$pubYear}</td>";
}

?>

以下是您的具体操作方法: 在MYSQL中链接两个表: 在MySQL中链接两个表非常容易,每个表中至少需要有一列(字段)彼此相似,才能将它们相互关联起来

例如:

<?php
// Assuming that $publications will hold all fetched values from researchprojects table
// Also assuming that $publi will hold all fetched data from publi table
$publi_ids = explode(",",$publications[0]['publications']);
//Now you have all ids in array in $publi_ids
//So moving forward with the code

foreach ($publi_ids  as $publi_id) {
echo "<h1>".$publi_id."</h1>";
$id = $publi_id;
$query = mysqli_query($mysqli,"SELECT * FROM publi WHERE publi_id='$id'");

$publi_data = array();
while ($rows = mysqli_fetch_array($query)) {
  $publi_data[] = $rows;
}

foreach ($publi_data as $publi_row) {
echo "Year :".$publi_row['publi_year']."</br>";
echo "</br></br></br>";
}
echo "</br></br></br></br>";  
} 
?>
800
Year : 2012
Year : 2013

900
Year : 2014
Year : 2015

1000
Year : 2016
Year : 2017
在您的情况下,两个表结构都需要这样:

Table : researchprojects => id,publications

Table : publi => publi_id,publi_year
您的案例:

<?php
// Assuming that $publications will hold all fetched values from researchprojects table
// Also assuming that $publi will hold all fetched data from publi table
$publi_ids = explode(",",$publications[0]['publications']);
//Now you have all ids in array in $publi_ids
//So moving forward with the code

foreach ($publi_ids  as $publi_id) {
echo "<h1>".$publi_id."</h1>";
$id = $publi_id;
$query = mysqli_query($mysqli,"SELECT * FROM publi WHERE publi_id='$id'");

$publi_data = array();
while ($rows = mysqli_fetch_array($query)) {
  $publi_data[] = $rows;
}

foreach ($publi_data as $publi_row) {
echo "Year :".$publi_row['publi_year']."</br>";
echo "</br></br></br>";
}
echo "</br></br></br></br>";  
} 
?>
800
Year : 2012
Year : 2013

900
Year : 2014
Year : 2015

1000
Year : 2016
Year : 2017
与您的情况一样,您的一行中提到的所有
id
,如下所示:

    researchprojects 
         |
         V
---------------------------
publications
800,900,1000
---------------------------

       publi 
         |
         V
---------------------------
publi_id     publi_year
800          2012
800          2013
900          2014
900          2015
1000         2016
1000         2017
---------------------------
显示表格数据输出的代码:

<?php
// Assuming that $publications will hold all fetched values from researchprojects table
// Also assuming that $publi will hold all fetched data from publi table
$publi_ids = explode(",",$publications[0]['publications']);
//Now you have all ids in array in $publi_ids
//So moving forward with the code

foreach ($publi_ids  as $publi_id) {
echo "<h1>".$publi_id."</h1>";
$id = $publi_id;
$query = mysqli_query($mysqli,"SELECT * FROM publi WHERE publi_id='$id'");

$publi_data = array();
while ($rows = mysqli_fetch_array($query)) {
  $publi_data[] = $rows;
}

foreach ($publi_data as $publi_row) {
echo "Year :".$publi_row['publi_year']."</br>";
echo "</br></br></br>";
}
echo "</br></br></br></br>";  
} 
?>
800
Year : 2012
Year : 2013

900
Year : 2014
Year : 2015

1000
Year : 2016
Year : 2017
我已经解决了这个问题

$query = "SELECT * FROM researchprojects";
$result = mysqli_query($con, $query);
while ($val = mysqli_fetch_array($result))
{
  $Id = $val['Id'];
  $Publications = $val['Publications'];
  $Publication_seprated = explode(',', $Publications);

  foreach($Publication_seprated as $Publication_seprated)
  {
  $query2 = "SELECT * FROM publi WHERE Pub_id = '$Publication_seprated'";
  $result2 = mysqli_query($con, $query2);
  while ($val2 = mysqli_fetch_array($result2))
        {
           $pub_id = $val2['pub_id'];
           $pub_year = $val2['pub_year'];

           echo $pub_year;
          } // while loop


    } // foreach

   } // while loop

这似乎不是MySQL表结构?:很抱歉,我找不到显示结构的方法,所以我将它放在htmlFix的第一个表中。请参阅规范化。谢谢草莓这是我想要显示的可能重复:谢谢,但是链接表然后在php中显示它的SQL查询是什么?谢谢你的回答,但是发布的id表和发布的publi_id表没有相同的值,因为发布有逗号分隔的值,所以如何我可以链接它们吗?嗯..相反,我认为您需要在publications表中每行添加一个发布,这样更好的做法是将所有发布放在一行中,这不是一个好的做法,您将在接下来的编码中遇到很多问题。。!你是对的,这会容易得多,但我不能分离这些值,因为它们与其他列相关,因此无法更改结构。不幸的是,我必须使用逗号分隔的值