我创建了一个在phpMyAdmin中完美运行的查询,但是当我尝试在.php文件中调用它时,我收到以下错误。
Undefined variable: mothers_name in C:\wamp\www\Family_Tree\showfamily.php on line 56
我的代码是:
$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;
我是否收到此错误,因为在通过mysql_query($ select_query)函数调用SQL之前,表“a”,“b”和“c”以及字段“mother_id”和“father_id”不存在。
第56行之前的代码查找,返回并显示结果。
答案 0 :(得分:0)
..._id = c.id" . // <-- you forgot a space, results in c.idWHERE
"WHERE a.id = " . $user_id;
答案 1 :(得分:0)
如果你从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;
这将让您使用非常方便的调试工具:
echo "[pre]" . $query . "[/pre]";
随着[&amp;实际上是&lt; &安培;取代。对不起,新来的SO。
然后,您将能够将实际查询从浏览器窗口复制/粘贴到phpMyAdmin中,并运行该查询并查看它们是否实际相同。