[英]Fetch data from same field name in two different tables and detect which filed name we mean
[英]How to query two tables at once for a field with the same name but different data?
我有兩個表,兩個表都包含一個名為“ id”的字段,每個表中都有不同的數據。 我需要檢索兩個ID來構造一個URL。 我正在使用以下代碼:
<?php // no direct access
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$value = $_POST['password'];
$content = mysql_query("SELECT * FROM jos_content WHERE pass='$value'") or die(mysql_error());
$menu = mysql_query("SELECT * FROM jos_menu WHERE menutype='mymenutype' AND alias='$value'") or die(mysql_error());
$id = mysql_fetch_array( $content );
$itemid = mysql_fetch_array( $menu );
// Print out the contents of each row into a table
if(isset($_POST['password'])){
header('Location: ' . 'index.php?option=com_content&view=article&id=' . $id['id'] . '&Itemid=' . $itemid['id']);
exit;
}
?>
但是,我僅從第一個查詢獲取數據,而第二個查詢則什么也不返回。 我做錯什么了嗎? 也許我的SELECT查詢的拼寫不正確? 請幫忙。
在此先感謝,S。
我看到的第一件事是您也在第二個查詢中使用了password
,我的直覺告訴我, password
並不是您想要的。
無論如何,為了調試查詢,首先要查看錯誤日志,看看是否or die(mysql_error())
部分拋出了異常。
如果沒有,則echo
顯$menu
字符串(查詢),完全按照將在mysql中顯示的方式運行該字符串,並查看是否返回一些記錄。 當它返回內容時,更改字符串,然后就可以了-它應該可以工作
您可以使用var_dump($ menu)查看調試結果。 如果$ itemid是多維數組,則必須正確使用$ itemid [0] ['id']之類的索引,並且您可以嘗試回顯查詢,並按照(Tudor Constantin)的建議在mysql中使用相同的查詢。 嘗試像這樣使用mysql joins
選擇jc.id作為id,選擇jm.itemid作為itemid從jos_content作為jc離開加入jos_menu作為jm在jm.alias = jc.pass上jm.menutype ='mymenutype'和jc.pass ='$ value'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.