查询加入同一个表3次

时间:2022-07-21 01:58:59

I have created a query that works perfectly in phpMyAdmin, however when I try to call it in a .php file I get the following error.

我创建了一个在phpMyAdmin中完美运行的查询,但是当我尝试在.php文件中调用它时,我收到以下错误。

Undefined variable: mothers_name in C:\wamp\www\Family_Tree\showfamily.php on line 56

My code is:

我的代码是:

$select_query = "SELECT a.id, CONCAT( a.surname,  ', ', a.first_names ) AS child_name, " . 
"CONCAT( b.surname,  ', ', b.first_names ) AS mothers_name, " .
"CONCAT( c.surname, ', ', c.first_names ) AS fathers_name " .
"FROM family_members a " .
"INNER JOIN family_members b ON a.mother_id = b.id " .
"INNER JOIN family_members c ON a.father_id = c.id" .
"WHERE a.id = " . $user_id;

Am I getting this error because the tables "a","b" and "c" and the fields "mother_id" and "father_id" doesn't exist until the SQL is called through the mysql_query($select_query) function.

我是否收到此错误,因为在通过mysql_query($ select_query)函数调用SQL之前,表“a”,“b”和“c”以及字段“mother_id”和“father_id”不存在。

The code before line 56 looks up, returns and displays the results as they should.

第56行之前的代码查找,返回并显示结果。

2 个解决方案

#1


0  

..._id = c.id" .               // <-- you forgot a space, results in c.idWHERE
"WHERE a.id = " . $user_id;

#2


0  

You might find this all much easier if you step back from the java style line breaking and attack it thusly. There's no need in PHP for splitting it up, and the extra punctuation makes is easier to make a mistake.

如果你从java风格的行中退一步并进行攻击,你可能会发现这一切都变得容易得多。 PHP中没有必要将它拆分,额外的标点符号更容易出错。

$select_query = 
"SELECT 
     a.id, 
     CONCAT( a.surname, ', ', a.first_names ) AS child_name, 
     CONCAT( b.surname, ', ', b.first_names ) AS mothers_name,
     CONCAT( c.surname, ', ', c.first_names ) AS fathers_name 
FROM family_members a 
INNER JOIN family_members b ON a.mother_id = b.id 
INNER JOIN family_members c ON a.father_id = c.id
WHERE a.id = " . $user_id;

This will let you use the very handy debugging tool of:

这将让您使用非常方便的调试工具:

echo "[pre]" . $query . "[/pre]";

With the [ & ] actually being < & >. Sorry, new to SO.

随着[&]实际上是<&>。对不起,新来的SO。

THEN, you'll be able to copy/paste the actual query from your browser window into phpMyAdmin, and run that query and see if they actually are identical.

那么,您将能够将实际查询从浏览器窗口复制/粘贴到phpMyAdmin中,并运行该查询并查看它们是否实际相同。

#1


0  

..._id = c.id" .               // <-- you forgot a space, results in c.idWHERE
"WHERE a.id = " . $user_id;

#2


0  

You might find this all much easier if you step back from the java style line breaking and attack it thusly. There's no need in PHP for splitting it up, and the extra punctuation makes is easier to make a mistake.

如果你从java风格的行中退一步并进行攻击,你可能会发现这一切都变得容易得多。 PHP中没有必要将它拆分,额外的标点符号更容易出错。

$select_query = 
"SELECT 
     a.id, 
     CONCAT( a.surname, ', ', a.first_names ) AS child_name, 
     CONCAT( b.surname, ', ', b.first_names ) AS mothers_name,
     CONCAT( c.surname, ', ', c.first_names ) AS fathers_name 
FROM family_members a 
INNER JOIN family_members b ON a.mother_id = b.id 
INNER JOIN family_members c ON a.father_id = c.id
WHERE a.id = " . $user_id;

This will let you use the very handy debugging tool of:

这将让您使用非常方便的调试工具:

echo "[pre]" . $query . "[/pre]";

With the [ & ] actually being < & >. Sorry, new to SO.

随着[&]实际上是<&>。对不起,新来的SO。

THEN, you'll be able to copy/paste the actual query from your browser window into phpMyAdmin, and run that query and see if they actually are identical.

那么,您将能够将实际查询从浏览器窗口复制/粘贴到phpMyAdmin中,并运行该查询并查看它们是否实际相同。