[英]If mysql value contains another mysql value from a different table, echo
我有兩個表,“客戶”和“用戶”。
在客戶表中,我有一列名為“名稱”的列。 這具有所有客戶名稱。
在我的用戶表中,我有一列名為“管理”。 它具有某些客戶名稱。
我想,如果“管理” 包含 “名”客戶的任何顯示內容。
這是一個例子
“名稱”包含客戶“ applebees”,“ johnny carinos”和“ pizza小屋”。
用戶rick是“管理”“ applebees”和“披薩小屋”。 在他的行/列中,它看起來像是“ applebees,pizza小屋”(我使用的是implode函數,這就是為什么有一個,”)
因此,在我的客戶頁面上,我希望rick看到applebees和披薩小屋。 我不想讓他見到約翰尼·卡里諾斯,因為他沒有管理。
我怎樣才能做到這一點? 我嘗試過preg match&strpos,但失敗了。 (我對php還是很陌生的)。 如果是單個值,這會容易得多,所以我可以只設置value = value。 不幸的是,擁有多個用於“管理”的值確實讓我感到困惑!
希望這有道理。 謝謝你的幫助!
這就是我現在擁有的(是的,我知道,這可能是非常錯誤的。)
<?php
$sql = "SELECT * FROM customers";
$result = mysql_query($sql);
$thecust = $_SESSION['user']['managing'];
$thecustomers = htmlentities($row['name']);
$check = strprk(wing, $thecust);
if ($check) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo "<a href='customer.php?id=" . $row['id'] . "'>" . $row['name'] . "</a>"?></td>
<td><?php echo htmlentities($row['contact_name'], ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlentities($row['contact_number'], ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlentities($row['contact_email'], ENT_QUOTES, 'UTF-8'); ?></td>
</tr>
<? } ?>
答案在於您的數據庫模式和查詢方式。 您並沒有真正進入數據庫表結構,但我建議您使用類似這樣的內容
users
-------
user_id INT, autoincrement, PRIMARY KEY
user_name VARCHAR
[other user-specific fields]
users_to_customers
-------
user_id INT
customer_id INT
[compound primary key across both fields]
customers
-------
customer_id INT, autoincrement, PRIMARY KEY
customer_name VARCHAR
[other customer specific fields]
注意我這里有三個表。 這使您可以將任意數量的用戶與任意數量的客戶相關聯。 現在獲取要查找的信息,假設您知道查看客戶頁面的用戶的user_id
。 您的查詢可能看起來像
SELECT c.customer_id, c.customer_name, [whatever other fields you need]
FROM users AS u
INNER JOIN users_to_customers AS uc
INNER JOIN customers AS c
WHERE u.user_id = ? [the user_id of the user viewing the page]
這將為您提供與該用戶相關的那些客戶的客戶信息。
您也不應該使用mysql_*
函數,因為它們已被棄用(請注意PHP.net文檔中的紅色大警告)。 也許使用mysqli_ *函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.