簡體   English   中英

從表1獲取ID以與表2匹配

[英]Get id from table 1 to match with table 2

我是php的新手,我對我想做什么有所了解,但我不確定如何執行它們

我在mysql表1中有2個表,其中包含:

ID|Name|AboutMe|Specialisation
Example:
101|Andrew|handsome|drawing
102|Jane|pretty|drawing
103|Daniel|cute|swimming

table 2 contains
ID|Link|Title
101|//image1.jpg|Drawing in the dark
102|//image2.jpg|Drawing in me
103|//image3.jpg|Fish swimming

我只想在我的網站運行后才顯示鏈接到專業化的圖像。

我所擁有的流量1)

// get all the ID with drawing as specialisation // will return 2 rows
select ID FROM table1 WHERE Specialisation = "Drawing";
id = ID

2)從匹配的每一行中,采用ID來獲取表2中的鏈接,如下所示

// get row of the links from the userid
Select * FROM userdatafiles WHERE ID = id
store the links into a variable and send to my jquery to display.

因此,基本上獲取與圖形匹配的每一行的ID,並從表2中獲取ID以找到相同的ID,並獲取鏈接,然后將其發送給jquery。

由於我必須發送很多行,並且需要使用數組和循環,所以我不確定該怎么做。 有人可以引導我了解我在做什么嗎? 謝謝!

更新:

jQuery的:

$.ajax({
    type: "POST",
    //dataType: "json",
    url: "CMS/PHP/displayFeatThumbs.php",
    success: function(data) {
            alert(data[0]);
    }
});

PHP:

<?php
include 'dbAuthen.php';
$sql = 'SELECT * FROM userdatafiles JOIN users ON userdatafiles.UserID = users.UserID WHERE Specialisation = "Interactive Art & Technology"';

$result = mysqli_query($con,$sql);


if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo $links[] = $row["Link"];           
    }
} else {
    echo "0 results";
}
?>

不知道如何在jquery上顯示數組索引,嘗試過alert(data [0]),alert(data.links [0]);

您可以使用自然聯接來連接2個或更多表。 查詢應如下所示:

SELECT * FROM userdatafiles JOIN table1 ON userdatafiles.id = table1.id WHERE Specialisation = "Drawing"

結果將是一個新表,如下例所示:

table1:id | 名稱| 日期
table2:id | 城市| 國家

結果:id | 名稱| 日期| 城市| 國家

您可以使用如下數據:

$result = mysql_query("Your Query");  
while($row = mysql_fetch_object($result)){  
            //Access values with $row->ColumnName  
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM