如何从数据库中获取价值

时间:2010-02-27 13:04:26

标签: php mysql

我想从数据库中获取价值。例如,在名称字段中,它显示存储在数据库中的名称。我想在相应的字段中显示值。但它无法检索值..plz guys..help me

<?php 
    session_start();
    $username = $_SESSION["username"];
    $department = $_SESSION["department"];

?>
<html>
<head>
<title>Change Password</title>

</head>

<form method="post"  action="changepassprocess.php">
<?php
$db = mysql_connect('localhost','root') 
    or die ("unable to connect");
    mysql_select_db('fyp',$db) or die ("able to select");

$sql_select = "SELECT * FROM access WHERE username ='".$username."' ";
?>

<font face= "arial" size="2" font color="black">
<center>
<h3 align=center> Change Password </h3>
<table width="500" height="100" border="0" cellspacing="0" cellpadding="2">

<tr>
      <tr>
           <td align="left">User ID</td>
           <td>: <input name="username" type="text" id="username" value="<? {echo "$username"; } ?>" size="20" maxlength="10" readonly='username'></td>
      </tr>  

      <tr>
        <td align="left">Name </td>
        <td>: <input name="name" type="text" id="name" value="<? {echo "$name"; } ?>" size="50" readonly="name"></td>
    </tr>       

         <tr>
           <td align="left">Department  </td>
           <td>: <?php echo $row['department']; ?> </td>      
         </tr>

         <tr>
           <td align="left">New Password </td>
           <td>:<input name="newpassword" type="password" id="newpassword" size="20" ></td>
      </tr>

</table><br>  
    <align = center><input type="submit" name="send" value="Change Password">
</form>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

好吧,您忘了将查询运行到数据库。 $ sql_select变量保存查询文本,但您需要将其传递给数据库并从中检索答案。阅读http://php.net/manual/en/function.mysql-query.php和那里的例子。

答案 1 :(得分:2)

你错过了:

$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);

这些将执行您准备的查询并将结果作为数组$row获取。

您可能想看看如何使用来自以下的PHP从Mysql DB获取值:

W3school: Select Data From a Database Table

答案 2 :(得分:1)

<?php 
session_start();
include("../connect.php");
$user=$_SESSION['user'];
if(empty($user))
{
    header("location:index.php");
}
else{
$query_display="SELECT * FROM user_login WHERE user_id_no='$user_id_no'";
$result=mysqli_query($bd,$query_display);
while($arr=mysqli_fetch_array($result))
{
$first_name=$arr['first_name'];
$last_name=$arr['last_name'];
$address=$arr['address'];
}
echo  $first_name;
echo $last_name;
echo $address;
}
?>
connect.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "";
$bd=mysqli_connect($mysql_hostname,$mysql_user,$mysql_password,$mysql_database);
?>