【问题标题】:PHP problems getting data from table in mysql... only showing column names as dataPHP问题从mysql中的表中获取数据...仅将列名显示为数据
【发布时间】:2013-10-08 02:00:02
【问题描述】:

我试过了,但对我没有用...我的意思是我正在拿到我的桌子,并且行数是正确的。但是,当我使用 $row['ColumnName']... 时,我不断获得列的实际名称,而不是数据本身。 如果我向表中添加更多行数据,我会增加行数,但数据仍然相同。只有列名。

有人遇到过这个问题吗?以及如何解决? php 版本:5.3 和 mysql 版本:5.5

$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('employeedb');

$query = "SELECT * FROM employee"
$result = mysql_query($query);

echo "<table>"; // start a table tag in the HTML

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['age'] . "</td></tr>";          
//$row['index']     the index here is a field name
}

echo "</table>"; //Close the table in HTML

mysql_close(); //Make sure to close out the database connection

【问题讨论】:

  • 使用 mysql_fetch_assoc 函数,因为它会给你一个关联数组,也考虑使用 mysqli 函数,因为 mysql api 已被贬值。它们的名称和用途几乎相同,因此切换不是那么乏味
  • @PatrickEvans 我完全同意mysqli 声明;但是,mysql_fetch_array() 也将返回关联值(默认情况下;第二个参数指定返回类型)。
  • 考虑使用mysqli_fetch_assoc。另外:mysql_ 已弃用,请考虑使用 mysqli 或 PDO。您的查询行中还缺少;
  • 我确实将我的代码更改为 PDO,因为许多人说它更好但仍然是同样的问题,我尝试使用 MySqli 也遇到同样的问题......如果代码是标准的,不知道出了什么问题。
  • 我想通了......我真是个白痴......我一直都有正确的答案...... SQL语句是问题......因为我习惯了SQL并且是编写 MYSql 代码我对查询的 Select 语句感到困惑……我想这就是为什么最好使用 SP 的原因……但是我修复了它……感谢大家的帮助。

标签: php mysql


【解决方案1】:

mysql_ 已弃用,这是 PDO 中的一些代码。

//Open PDO connection
try {
    $db = new PDO("mysql:dbname={'employeedb'}; host={'localhost'}", 'root', '');
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (PDOException $Exception) {
    echo "There was an error.";
}
//Query prep
$query = $db->prepare("
    SELECT *
        FROM employee
");
//Query execution; try to use prepared statements always
$query->execute();    
//create string $output
$output = "<table>";
//for rows of indeterminable quantity, use foreach()
foreach($query as $table) {
    //append to $output
    $output .= "<tr><td>";
    $output .= htmlspecialchars($table["name"]);
    $output .= "</td><td>";
    $output .= htmlspecialchars($table["age"]);
    $output .= "</td></tr>";          
}    
$output .= "</table>";
//print $output
echo $output;
//close PDO connection
$db = null;

顺便说一句,请确保使用htmlspecialchars() 转义所有输出。我还会考虑使用某种try/catch 复合体,以确保正确处理错误并且不会向用户透露相关信息。

哦,看来你解决了。如果您在未定义的行数内工作,请考虑使用foreach() 循环,而不是使用while() 循环。

【讨论】:

    【解决方案2】:

    你可以尝试使用数字索引而不是列名。如下图所示

      while($row = mysql_fetch_array($result)){  
    
    echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td></tr>";          
    
      }
    

    【讨论】:

    • 谢谢,但这并不是问题所在:P 有人对你投了反对票……但我提供了一个更新,让你回到 0。谢谢你的帮助
    【解决方案3】:

    试试这个:

    $db = new PDO ('localhost', 'root');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $query = "SELECT * FROM employee"
    $statement = $db->prepare($query);
    $statement->execute();
    echo '<table>';
        foreach($statement as $row){
            echo '<tr>';
              echo '<td>', $row['name'],'</td>';
              echo '<td>',$row['age'],'</td>';
            echo '</tr>';
        }
    echo '</table>';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 2019-08-03
      • 2018-10-25
      • 1970-01-01
      • 2019-04-12
      相关资源
      最近更新 更多