Php 如何从表1中获取特定数据,请在表2中使用
我试图得到表1(stellingen)中的特定行。我想存储这些行,以便为第二个表(stelling)指定我感兴趣的行。假设表1有5行,其中stelling ID与REGIOID=5匹配。这些ID来自我想要用来从第二个表中获取数据的stelling ID。查看代码以查看我尝试的内容。我没有设法找到一个方法来实现这一点 所以,也许太清楚了,因为人们总是说我不清楚: 有两张桌子。它们都有一个匹配的列。我试图告诉第二个表我需要数据,但前提是它与第一个表的数据匹配。就像一根树枝。然后,我想输出第二个表中的一些数据 我以前试过这样的方法:Php 如何从表1中获取特定数据,请在表2中使用,php,database,mysqli,while-loop,Php,Database,Mysqli,While Loop,我试图得到表1(stellingen)中的特定行。我想存储这些行,以便为第二个表(stelling)指定我感兴趣的行。假设表1有5行,其中stelling ID与REGIOID=5匹配。这些ID来自我想要用来从第二个表中获取数据的stelling ID。查看代码以查看我尝试的内容。我没有设法找到一个方法来实现这一点 所以,也许太清楚了,因为人们总是说我不清楚: 有两张桌子。它们都有一个匹配的列。我试图告诉第二个表我需要数据,但前提是它与第一个表的数据匹配。就像一根树枝。然后,我想输出第二个表中的
SELECT
*
FROM
table2
LEFT JOIN
table1 ON
table1.ID = table2.table1_id
我尝试创建一个while循环来获取之前的数据(在第一个if语句之后,最后一个+=用于变量$amountofstellinge):
$amountOfStellinge=0;
而($amountoftellinge如果我理解正确:
SELECT
*
FROM
stelling
LEFT JOIN
stellingen
ON
stelling.stellingID = stellingen.stelling_id
WHERE
stellingen.REGIOID=1
ORDER BY stelling.Timer DESC LIMIT 5 ;
这看起来很有趣,我要试试。除了我的问题@noobjsmysql抛出的错误外,我还要把我试过的东西放在DESC位置。我现在编辑了mysql,没有任何错误。你只能用mysql解决。编辑你的问题,显示示例数据、表结构和预期结果。@LelioFaieta谢谢你,Lelio,我忘了写e预期结果。使用文本而不是图片。顺便说一句,这样的图片是不可能阅读的
if($result = mysqli_query($con, "SELECT * FROM stellingen WHERE REGIOID=1;")) {
$row = mysqli_fetch_assoc($result);
$stellingid= $row["Stelling_ID"];
//checking.. and the output is obviously not what I want in my next query
printf($stellingid);
//defining the variable
$Timer=0;
$sql1="SELECT * FROM stelling WHERE stelling_iD=$stellingid ORDER BY Timer DESC;";
$records2 = mysqli_query($con, $sql1);
$recordscheck = mysqli_num_rows($records2);
//max 5 data
if ($recordscheck < 5){
while ($stelling = mysqli_fetch_assoc($records2)){
//At the end, i would like to only have the data that is most recent
$Timer = date('d F', strtotime($stelling['Timer']));
echo "<p><div style='color:#ED0887'>".$Timer.":</div><a target = '_blank' style='text-decoration:none' href='".$stelling['Source']."'>".$stelling['Title']."</a></p>";
}}
$recordscheck+=1; } // this is totally wrong
$Timer=0;
$sql1="SELECT
*
FROM
stelling
LEFT JOIN
stellingen
ON
stelling.ID = stellingen.stelling_id
WHERE
stellingen.REGIOID=1
ORDER BY stelling.Timer LIMIT 5 DESC ;";
$records2 = mysqli_query($con, $sql1);
printf($records2);
while ($stelling = mysqli_fetch_assoc($records2)){
$Timer = date('d F', strtotime($stelling['Timer']));
echo "<p><div style='color:#ED0887'>".$Timer.":</div><a target = '_blank' style='text-decoration:none' href='".$stelling['Source']."'>".$stelling['Title']."</a></p>";
}
SELECT
*
FROM
stelling
LEFT JOIN
stellingen
ON
stelling.stellingID = stellingen.stelling_id
WHERE
stellingen.REGIOID=1
ORDER BY stelling.Timer DESC LIMIT 5 ;