简体   繁体   English

在PHP中将$ row [$ variable]与LEFT JOIN一起使用时,结果为空

[英]Empty results when using $row[$variable] with LEFT JOIN in PHP

I'd like to begin by saying that this is my first question here at stack and I apologize in advance if this question has been answered before, however I have been so far unable to find an answer or fix it myself. 首先,我想说的是,这是我的第一个问题,如果之前已经回答过这个问题,我会为此道歉,但是到目前为止,我还无法自己找到答案或解决问题。

I am trying to use the SELECT function in a php file to run a basic report. 我正在尝试在php文件中使用SELECT函数来运行基本报告。 I wrote the SQL in PHPMyAdmin and used the convert-to-php button to do just that. 我用PHPMyAdmin编写了SQL,并使用convert-to-php按钮来完成此操作。 What I get is the following: 我得到的是以下内容:

SELECT
l.id AS 'ID',
l.type AS 'Type',
l.state AS 'State',
l.won_at AS 'Won',
l.lost_at AS 'Lost',
l.cancelled_at AS 'Cancelled',
l.created_at AS 'Created',
l.source AS 'Source',
u.first_name AS 'Owner First Name',
u.last_name AS 'Owner Last Name'
FROM `leads` AS l
LEFT JOIN `users` AS u ON
(u.`id` = l.`owner_id`)
LEFT JOIN `regions` AS rg ON
(rg.`id` = l.`region`)
WHERE l.`state` IS NOT NULL
[...]";

When I put this into a PHP document it looks like this: 当我将其放入PHP文档时,它看起来像这样:

    <?php
// Create connection
$con=mysqli_connect("localhost","root","pass","database");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// And now for the good stuff

$result = mysqli_query($con,"SELECT
l.id AS 'LeadID',
l.type AS 'Type',
l.state AS 'State',
l.won_at AS 'Won',
l.lost_at AS 'Lost',
l.cancelled_at AS 'Cancelled',
l.created_at AS 'Created',
l.source AS 'Source',
u.first_name AS 'First Name',
u.last_name AS 'Last Name'
FROM `leads` AS l
LEFT JOIN `users` AS u ON
(u.`id` = l.`owner_id`)
WHERE l.`state` IS NOT NULL
");

echo "<table border='1'>
<tr>
<th>Test1</th>
<th>Test2</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row["$LeadID"] . "</td>";
  echo "<td>" . $row["$l.type"] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

What seems to be happening is that I am able to create the table with no issue only when there is only one database being selected, and when I use LEFT JOIN, I have so far been unable to find a way to change the $row["$variable"] input to something that will work. 似乎正在发生的事情是,只有在只选择了一个数据库的情况下,我才能够创建表,而当我使用LEFT JOIN时,到目前为止,我一直找不到改变$ row [的方法。 “ $ variable”]输入将起作用的东西。

I know that the data is there and I know that the connection works, it's just the LEFT JOIN that is giving me a bit of trouble. 我知道数据在那里,而且我知道连接有效,只是LEFT JOIN给我带来了一些麻烦。

Any help on this would be greatly appreciated! 任何帮助,将不胜感激!

use: 采用:

echo "<td>" . $row["LeadID"] . "</td>";
echo "<td>" . $row["Type"] . "</td>";

You don't need $ , these are just literal strings not the values of variables. 您不需要$ ,这些只是文字字符串,而不是变量的值。 And the keys are case-sensitive, so you have to use Type , not type . 而且密钥区分大小写,因此您必须使用Type而不是type

I think its better to use foreach instead of while like this: 我认为最好使用foreach而不是像这样:

$rows = mysqli_fetch_array($result);
foreach ($rows as $value)
{
  echo "<tr>";
  echo "<td>" . $value["LeadID"] . "</td>";
  echo "<td>" . $value["Type"] . "</td>";
  echo "</tr>";
}
echo "</table>";

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

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