0

首先我想说这是我在堆栈上的第一个问题,如果这个问题之前已经回答过,我提前道歉,但是到目前为止我无法找到答案或自己解决它。

我正在尝试在 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 给我带来了一些麻烦。

对此的任何帮助将不胜感激!

4

2 回答 2

1

利用:

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

您不需要$,这些只是文字字符串而不是变量的值。并且键是区分大小写的,所以你必须使用Type,而不是type

于 2013-09-29T21:18:27.010 回答
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>";
于 2013-09-29T21:22:13.933 回答