如何使我的查询输出行值而不是字段值

时间:2016-02-08 08:28:45

标签: php mysql sql

以下是我的代码。 它输出字段名称而不是行值。

请问如何输出我在数据库中的行?

<?php 
$conn_error = "Could not connect";
$correct = "all correct!";
$host = "localhost";
$username = "Ifacool";
$password = "1234";

$mysql_db = "ifacool";

if (!mysql_connect($host, $username, $password) || !mysql_select_db                      ($mysql_db)) {
die ($conn_error);
}
else{
       echo $correct . '<br>'; 
}
$query = "SELECT 'firstname', 'password' FROM ifacooltable WHERE ID = 1 ";
if ($query_run = mysql_query($query)) {
    while ($query_row = mysql_fetch_assoc ($query_run)){
        $firstname = $query_row['firstname'];
        $password = $query_row['password'];
        echo $firstname . ' password is ' . $password . ' exactly.<br>';
    }
} else {
    echo "query failed";
}
?>

1 个答案:

答案 0 :(得分:3)

使用反引号`而不是单引号'来转义MySQL中的标识符 - 现在您只需选择纯字符串:

$query = "SELECT `firstname`, `password` FROM `ifacooltable` WHERE `ID` = 1 ";