从MySql数据库中获取值到表单中

时间:2015-05-15 10:01:51

标签: php mysql

我试图从MySql数据库中获取值。我有一个表单,我想从MySql数据库中获取emailid。请帮忙

<tr><td><b>Email :</td><td><input type="text" name="txtemail" readonly value="<?php
$name=$_SESSION['usr_name'];
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 

$conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 

mysqli_select_db($conn,$db_name);

$result = mysqli_query($conn,"SELECT emailid FROM tabledetails WHERE    
username='$name'");

echo $result;
mysqli_close($conn);
?>">
</td>
</tr>

3 个答案:

答案 0 :(得分:1)

这在我的本地系统中完美运行:

根据您的要求尝试此代码:

<?php
$name=$_SESSION['usr_name']
$servername="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$dbname="test"; // Database name 
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
else{
echo "Connected successfully";
}

$res  = mysqli_query($conn,"SELECT emailid FROM tabledetails WHERE username='$name'");

while($row=mysqli_fetch_assoc($res))
{
$rows = $row['email'];
 print_r $rows;
 echo $rows;   
}
?>
<tr><td><b>Email :</td><td><input type="text" name="txtemail" readonly value="<?php echo $rows ?>"/>

<强>输出: -

Connected successfully
Email :User

答案 1 :(得分:0)

删除此行。

mysqli_select_db($conn,$db_name);

并将第4个参数添加到mysqli_connect

$conn=mysqli_connect($host,$username,$password, $db_name) or die("cannot connect"); 

了解更多细节 http://www.w3schools.com/php/php_mysql_select.asp

答案 2 :(得分:-1)

试试这个:

while($row = $result->fetch_assoc()) {
        echo $row["Email"];
    }