gpt4 book ai didi

php - 试图从数据库中回显包含多列的单行

转载 作者:行者123 更新时间:2023-11-29 05:24:10 24 4
gpt4 key购买 nike

我正处于用户进程的末尾,我似乎无法在包含 10 列的单行中回显所有数据。这是我的代码:

$state = $_POST['state'];

$city = $_POST['city'];

$jobTitle = $_POST['jobTitle'];



try {
$db = new PDO("mysql:host=localhost;dbname=TeacherData;port=8889","censored","censored");
} catch (Exception $e) {
echo "Could not connect to database.";
exit;
}




$sql = "SELECT
TOT_EMP,
H_MEAN,
JOBS_1000,
A_MEAN,
H_PCT10,
H_MEDIAN,
H_PCT90,
A_PCT10,
A_MEDIAN,
A_PCT90
FROM teacherdataall
WHERE PRIM_STATE='" . $state . "'
AND AREA_NAME='" . $city . "'
AND OCC_TITLE='" . $jobTitle . "'";

$result = $db->query($sql);

if($result !== false)
{
$cols = $result->columnCount(); // Number of returned columns

echo 'Number of returned columns: '. $cols. '<br />';

foreach($result as $row)
{
echo $row['TOT_EMP'] . ' - ' . $row['H_MEAN'] . ' - '. $row['JOBS_1000'] . ' - ' . $row['A_MEAN'] . ' - '. $row['H_PCT10'] . ' - ' . $row['H_MEDIAN'] . ' - '. $row['H_PCT90'] . $row['A_PCT10'] . ' - ' . $row['A_MEDIAN'] . ' - ' . $row['A_PCT90'] . '<br />';
}
}

无论 print/echo 函数的什么变化,我都无法让数据出现!只有一排!!非常感谢任何帮助。

这是第 3 页的代码:

<?php
$state = $_POST['state'];

$city = $_POST['city'];

try {
$db = new PDO("mysql:host=localhost;dbname=TeacherData;port=8889","root","root");
} catch (Exception $e) {
echo "Could not connect to database.";
exit;
}

$stmt = $db->prepare("SELECT DISTINCT OCC_TITLE FROM teacherdataall WHERE PRIM_STATE='" . $state . "' AND AREA_NAME='" . $city . "';");


echo "<br>";
echo "<form action='fourth_page.php' method='post'>";
echo "State: ";
echo "<select name='state'><option value=$state>$state</option>";
echo "</select>";
echo "City: ";
echo "<select name='city'><option value=$city>$city</option>";
echo "</select>";
echo "<select name='jobTitle'><option value=''>-- Job Title --</option>";

if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo '<option value="' . $row['OCC_TITLE'] . '">' . $row['OCC_TITLE'] . '</option>';
echo "<hr>";
}
echo "</select>";
echo "<button type='submit'>Next</button>";
}
echo "</form>";



?>

完成编辑:由于没有在 html value=$city 周围添加单引号,我从 $_POST 传递了一个错误的值。现在一切都修好了!谢谢@Ray!

最佳答案

colCount() 告诉您请求的列数,而不是您返回的结果。您可能需要 rowCount(),因为您已经知道在查询中明确设置了多少列。在您的情况下,您的选择语句具有:TOT_EMP、H_MEAN、JOBS_1000、A_MEAN、H_PCT10、H_MEDIAN、H_PCT90、A_PCT10、A_MEDIAN、A_PCT90。这意味着对于您在 SQL 查询中指定的数字列,您总是会返回第 10 个数字。

综上所述,我怀疑您的查询运行成功,但返回一组空结果(无匹配项)。尝试手动运行您在 $sql 中构建的查询。

如果您想将结果视为关联数组,您还需要从结果中获取行:

 $rowCount = $result->rowCount(); 

echo 'Number of returned results: '. $rowCount. '<br />';

$rows = $result->fetchAll(PDO::FETCH_ASSOC);


foreach($rows as $row)
{
//... rest here

我最后要注意的是,你不应该在你的查询中直接使用 $_POST 数组数据,除非你真的想要 SQL 注入(inject)。使用准备好的语句并绑定(bind)、绑定(bind)、绑定(bind)您通往幸福的方式:

$sql = "SELECT TOT_EMP, H_MEAN, JOBS_1000,A_MEAN,H_PCT10, H_MEDIAN,H_PCT90, A_PCT10,
A_MEDIAN, A_PCT90 FROM teacherdataall WHERE PRIM_STATE=:state AND
AREA_NAME=:area AND OCC_TITLE=:jobTitle";


$area = "$city, $state";
$statement = $db->prepare($sql);
$statement->bindValue(":state", $state);
$statement->bindValue(":area", $area);
$statement->bindValue(":jobTitle", $jobTitle);

$result = $statement->execute();

关于php - 试图从数据库中回显包含多列的单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22104905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com