PHP从mysql中的表中获取数据时出现问题。。。仅将列名显示为数据

PHP从mysql中的表中获取数据时出现问题。。。仅将列名显示为数据,php,mysql,Php,Mysql,我试过了,但对我无效…我的意思是,我得到了我的表,行数是正确的。但是当我使用$row['ColumnName']时。。。我一直在获取列的实际名称,而不是数据本身。 如果我向表中添加更多的数据行,我会得到行数,但数据仍然相同。只有列名 有人有过这个问题吗?如何解决呢? php版本:5.3和mysql版本:5.5 $connection = mysql_connect('localhost', 'root', ''); mysql_select_db('employeedb'); $query =

我试过了,但对我无效…我的意思是,我得到了我的表,行数是正确的。但是当我使用$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
$connection=mysql\u connect('localhost','root','');
mysql_select_db('employeedb');
$query=“从员工中选择*
$result=mysql\u query($query);
回声“;//在HTML中启动一个表标记
而($row=mysql\u fetch\u array($result)){//创建一个循环来循环结果
回显“$row['name']”。$row['age']”;
//$row['index']此处的索引是字段名
}
回声“//用HTML关闭表格
mysql_close()//确保关闭数据库连接

mysql\uUcode>已被弃用,下面是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;
//打开PDO连接
试一试{
$db=new-PDO(“mysql:dbname={'employeedb'};host={'localhost'},'root','');
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常);
}捕获(PDO异常$Exception){
echo“有一个错误。”;
}
//查询准备
$query=$db->prepare(“
挑选*
来自员工
");
//查询执行;尽量使用事先准备好的语句
$query->execute();
//创建字符串$output
$output=“”;
//对于数量不确定的行,使用foreach()
foreach($queryas$table){
//追加到$output
$output.=“”;
$output.=htmlspecialchars($table[“name”]);
$output.=“”;
$output.=htmlspecialchars($table[“age”]);
$output.=“”;
}    
$output.=“”;
//打印$output
echo$输出;
//关闭PDO连接
$db=null;
另外,请确保使用
htmlspecialchars()
转义所有输出。我也会考虑使用某种<代码>尝试/catch < /C>复杂,以确保正确地处理错误,而相关的信息不会向用户透露。
看来你解决了。如果您在一个未定义的行中工作,而不是使用<代码>(或)<代码>循环,请考虑<代码>前述()/代码>循环。“

<代码> 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;
//打开PDO连接
试一试{
$db=new-PDO(“mysql:dbname={'employeedb'};host={'localhost'},'root','');
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常);
}捕获(PDO异常$Exception){
echo“有一个错误。”;
}
//查询准备
$query=$db->prepare(“
挑选*
来自员工
");
//查询执行;尽量使用事先准备好的语句
$query->execute();
//创建字符串$output
$output=“”;
//对于数量不确定的行,使用foreach()
foreach($queryas$table){
//追加到$output
$output.=“”;
$output.=htmlspecialchars($table[“name”]);
$output.=“”;
$output.=htmlspecialchars($table[“age”]);
$output.=“”;
}    
$output.=“”;
//打印$output
echo$输出;
//关闭PDO连接
$db=null;
另外,请确保使用
htmlspecialchars()
转义所有输出。我也会考虑使用某种<代码>尝试/catch < /C>复杂,以确保正确地处理错误,而相关的信息不会向用户透露。
看来你解决了。如果你在一个未定义的行中工作,而不是使用<代码>()(代码)>循环,考虑一个<代码>前段()/Cult>循环。< /P> < P>可以尝试使用数值索引而不是CououMn名称。如下图所示

  while($row = mysql_fetch_array($result)){  

echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td></tr>";          

  }
while($row=mysql\u fetch\u array($result)){
回显“$行[0]”.$行[1]”;
}

您可以尝试使用数字索引而不是冒号名称吗。如下图所示

  while($row = mysql_fetch_array($result)){  

echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td></tr>";          

  }
while($row=mysql\u fetch\u array($result)){
回显“$行[0]”.$行[1]”;
}
试试这个:

$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>';
$db=newpdo('localhost','root');
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常);
$query=“从员工中选择*
$statement=$db->prepare($query);
$statement->execute();
回声';
foreach($row形式的报表){
回声';
回显“”,$row['name'],'';
echo“”,$row['age'],'';
回声';
}
回声';
试试这个:

$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>';
$db=newpdo('localhost','root');
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常);
$query=“从员工中选择*
$statement=$db->prepare($query);
$statement->execute();
回声';
foreach($row形式的报表){
回声';
回显“”,$row['name'],'';
echo“”,$row['age'],'';
回声';
}
回声';

使用
mysql\u fetch\u assoc
函数,因为它将为您提供一个关联数组,还可以考虑在mysql api贬值时使用mysqli函数。它们在名称和用途上几乎相同,所以切换并不是那么简单tedious@PatrickEvans我完全同意
mysqli
声明;但是,也会返回关联值(默认情况下,第二个参数指定返回类型)。考虑使用<代码> MySqLyFutChyasAsOcs/Cuff>。此外: MySqL<是不推荐的,考虑MyQuLi或PDO。您还缺少一个
在你的查询行中。我确实将我的代码更改为PDO,因为很多人说它更好,但仍然存在相同的问题,我尝试使用MySqli也有相同的问题。。。不知道怎么了,如果代码是标准的。我想出来了。。。我真是个白痴。。。我一直都有正确的答案。。。SQL语句是问题所在。。。因为我已经习惯了SQL,并且正在编写MYSql代码,所以我对查询的Select语句感到困惑。。。我想这就是为什么最好使用SP的原因。。。但是我把它修好了。。。感谢大家的帮助。请使用
mysql\u fetch\u assoc
函数,因为它将为您提供一个关联数组,还可以考虑使用mysqli函数,因为mysql api是depr