首先我想说这是我在堆栈上的第一个问题,如果这个问题之前已经回答过,我提前道歉,但是到目前为止我无法找到答案或自己解决它。
我正在尝试在 php 文件中使用 SELECT 函数来运行基本报告。我在 PHPMyAdmin 中编写了 SQL,并使用了 convert-to-php 按钮来做到这一点。我得到的是以下内容:
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
[...]";
当我将它放入 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);
?>
似乎正在发生的事情是,只有当只选择一个数据库时,我才能毫无问题地创建表,并且当我使用 LEFT JOIN 时,到目前为止我一直无法找到更改 $row[ 的方法"$variable"] 输入到可以工作的东西。
我知道数据在那里,并且我知道连接有效,只是 LEFT JOIN 给我带来了一些麻烦。
对此的任何帮助将不胜感激!