我有一个简单的PHP代码,只显示是否已建立与数据库的连接。它有一个HTML部分,但似乎没有建立连接。使用的数据库是MySql。这些表不是作为程序的一部分创建的。
需要一些建议......
提前谢谢....
这是代码......
<h1>LOGIN PAGE </h1>
<form action="" method="post">
Username <input name="usname" type="text" />
Password <input name="paswd" type="password" />
<input name="submit" type="submit" />
</form>
<?php
if (isset($_POST['submit'])) {
$uname = $_POST['usname'];
$pswd = $POST['paswd'];
$conn = mysqli_connect("localhost","root","archana","details");
if(mysqli_connect_errorno())
{
echo "Failed to connect to Mysql" ;
}
else
{
echo "connection established";
}
$data = mysqli_query($conn, "SELECT * FROM user");
while($a = mysqli_fetch_array($data))
{
echo $a['username'];
}
mysqli_close($conn);
}
?>
答案 0 :(得分:0)
我根据Marcel Balzer评论重写您的代码
<h1>LOGIN PAGE </h1>
<form action="" method="post">
Username <input name="usname" type="text" />
Password <input name="paswd" type="password" />
<input name="submit" type="submit" />
</form>
<?php
if (isset($_POST['submit'])) {
$uname = $_POST['usname'];
$pswd = $POST['paswd'];
$conn = mysqli_connect("localhost","root","archana","details");
if(mysqli_connect_errno())
{
echo "Failed to connect to Mysql. Error: ".mysqli_connect_errno() ;
}
else
{
echo "connection established";
}
$data = mysqli_query($conn, "SELECT * FROM user");
while($a = mysqli_fetch_array($data))
{
echo $a['username'];
}
mysqli_close($conn);
}
?>