我首先要说的是,这是我在堆栈的第一个问题,如果此问题已经得到解答,我会提前道歉,但是到目前为止我还没有找到答案或自己解决。
我试图在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给我带来了一些麻烦。
对此的任何帮助将不胜感激!
答案 0 :(得分:1)
使用:
echo "<td>" . $row["LeadID"] . "</td>";
echo "<td>" . $row["Type"] . "</td>";
您不需要$
,这些只是文字字符串而不是变量的值。密钥区分大小写,因此您必须使用Type
,而不是type
。
答案 1 :(得分:0)
我认为最好使用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>";