开发者

how to get value from the database

i want to get value from database..for exmaple,in the name field, it show the name that stored in the database. i want to show the value in the respective field.but it cannot retrieve the value..plz guys..help me

<?php 
    session_start();
    $username = $_SESSION["username"];开发者_如何学Go
    $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>


Well, you forgot to run your query to the database. The $sql_select variable holds the query text, but you need to pass it to the database and retrieve the answer from it. Read http://php.net/manual/en/function.mysql-query.php and examples there.


You are missing:

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

These will execute the query you've prepared and get the results as an array $row.

You might want to see how get fetch a value from Mysql DB using php from:

W3school: Select Data From a Database Table.


<?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);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜