尝试使用数据库中的多个列回显单个行

时间:2014-02-28 20:10:57

标签: php mysql

我正处于用户进程的末尾,我似乎无法用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 />';
        }
}

无论打印/回声功能的变化如何,我都无法显示数据!这只是一排!!非常感谢任何帮助。

以下是第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!

2 个答案:

答案 0 :(得分:3)

colCount()告诉您请求的列数而不是您返回的结果。您需要rowCount(),因为您已经知道在查询中明确设置了多少列。在您的情况下,您的select语句包含: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注入。使用预备语句并绑定,绑定,绑定你的幸福方式:

$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();

答案 1 :(得分:0)

我目前正在寻找这个,很难完成上面最喜欢的选择。我发现有人发布了类似的问题,它对我有用,它可以获取sql的下拉列表。

我希望这对寻求稍微不同答案的人有所帮助。

This is the code i used and on the first try my problem was fixed.