简体   繁体   English

如何一次查询两个表以查找具有相同名称但数据不同的字段?

[英]How to query two tables at once for a field with the same name but different data?

I have two tables, both containing a field named "id" that has different data in each table. 我有两个表,两个表都包含一个名为“ id”的字段,每个表中都有不同的数据。 I need to retrieve both ids to construct an URL. 我需要检索两个ID来构造一个URL。 I'm using the following code: 我正在使用以下代码:

<?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;
}

?>

However, I get data only from the first query, while the second returns nothing. 但是,我仅从第一个查询获取数据,而第二个查询则什么也不返回。 Am I doing anything wrong? 我做错什么了吗? Perhaps my SELECT query is not spelled correctly? 也许我的SELECT查询的拼写不正确? Please, help. 请帮忙。

Thanks in advance, S. 在此先感谢,S。

First thing that I see is that you use the password in your second query also, which my intuition tells me is not quite what you want. 我看到的第一件事是您也在第二个查询中使用了password ,我的直觉告诉我, password并不是您想要的。

Anyway, in order to debug the query, first, have a look in the error log and see if or die(mysql_error()) part is throwing something. 无论如何,为了调试查询,首先要查看错误日志,看看是否or die(mysql_error())部分抛出了异常。

If not, echo the $menu string (query), run that exactly as it will be shown into mysql and see if returns some records. 如果没有,则echo$menu字符串(查询),完全按照将在mysql中显示的方式运行该字符串,并查看是否返回一些记录。 When you make it return something, change the string, and there you go - it should work 当它返回内容时,更改字符串,然后就可以了-它应该可以工作

You can use var_dump($menu) to see the results for debuging. 您可以使用var_dump($ menu)查看调试结果。 if $itemid is a multidimensional array u have to use index properly like $itemid[0]['id'] and you can try echo the query and and use same query in mysql as (Tudor Constantin) suggested. 如果$ itemid是多维数组,则必须正确使用$ itemid [0] ['id']之类的索引,并且您可以尝试回显查询,并按照(Tudor Constantin)的建议在mysql中使用相同的查询。 try to use mysql joins like this 尝试像这样使用mysql joins

select jc.id as id, jm.itemid as itemid FROM jos_content as jc left join jos_menu as jm on jm.alias=jc.pass WHERE jm.menutype='mymenutype' and jc.pass='$value' 选择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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM