简体   繁体   English

MySQL查询从命令行工作,但只有一半在PHP中工作

[英]Mysql query working from command line but only half working in PHP

I have two tables 我有两张桌子

1.owners 1.所有者
id ID
firstname 名字
lastname

2.product 2.产品
id ID
id2 id2
id3 id3
item 项目

SELECT * FROM owners LEFT JOIN product ON product.id=owners.id where firstname='Jezebel';

Works fine from the command line returning all relevant items from owners and product but using the following PHP 从命令行返回正常,可以从所有者和产品中返回所有相关项,但是使用以下PHP

$result = mysql_query("SELECT * FROM owners LEFT JOIN product ON product.id=owners.id where firstname='".$_POST['fname']."'")

only returns results from the table owners. 只返回表所有者的结果。

I have googled extensively and don't see anyone else with this problem. 我已经在Google上进行了广泛的搜索,没有看到其他人遇到此问题。

$fname= $_POST['fname'];
$result = mysql_query("SELECT * FROM owners LEFT JOIN product ON product.id=owners.id where firstname='$fname'")

First, make your query similar to this 首先,使您的查询与此类似

    $query = sprintf("SELECT * FROM owners LEFT JOIN product ON product.id=owners.id where firstname='%s'",mysql_real_escape_string(trim($_POST['fname'])));
    $result = mysql_query($query) or die(mysql_error());
    var_dump(mysql_fetch_assoc($result));

and let me know the result so I can update this answer. 并告诉我结果,以便我更新此答案。

First of all: You have two table which match on autoincrement column id ? 首先:您有两个表,它们的自动增量列id匹配?

However, try this? 但是,尝试这个吗?

SELECT tb1.id, tb1. firstname, tb1.lastname, tb2.id, tb2.id2, tb2.id3, tb2.item FROM owners AS tb1 LEFT JOIN product AS tb2 ON tb2.id=tb1.id where tb1.firstname=$fname

I have found the error. 我发现了错误。 It was nothing to do with the select statement. 这与select语句无关。 I had a typo in the display code. 我在显示代码中有错字。 It should have been like this 应该是这样的

echo "</td><td>"; 
echo $row['id3'];

But was like this instead 但是却是这样

echo "</td></td>"; 
echo $row['id3'];

Thank you all for your help. 谢谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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