无法将php连接到mysql

时间:2014-05-05 09:46:28

标签: php mysql

我已经建立了一个使用php和mysql登录的练习网站。但是,当我点击登录时(我已经在mysql中创建了用户名和密码),而是打开了php文件,这不应该发生。 我的html文件:

<form method="POST" action="connectivity.php">
Username<br><input type="text" name="username" size="40"><br><br>
Password<br><input type="password" name="password" size="40"><br>
<input id="button" type="submit" name="submit" value="Sign In">

我的php文件:

define ("DB_HOST","localhost");
define ("DB_NAME","sleepy");
define ("DB_USER","root");
define ("DB_PASSWORD","panda");

$con=mysql_connect (DB_HOST,DB_USER,DB_PASSWORD) or die ("Failed to connect to MySQL: " .mysql_error());

$db=mysql_select_db(DB_NAME,$con) or die ("Failed to connect to MySQL: " .mysql_error());

/*
$ID = $_POST['username'];
$Password = $_POST['password'];
*/

function SignIn()
{
session_start();  
if(!empty($_POST['username']))
{
$query= mysql_query ("SELECT* FROM Username where username ='$_POST[username]' AND password = '$_POST[password]'",$db) or die (mysql_error());
$row= mysql_fetch_array($query) or die (mysql_error());
if (!empty($row['username']) AND !empty($row['password']))
{
$_SESSION['username'] = $row['password'];
echo "Successfully Signed In.";
}
else
{echo "Sorry, you entered the wrong username and password. Please Try Again.";}
}
}
if (isset($_POST['submit']))
{SignIn();}

mysql_close($con);

4 个答案:

答案 0 :(得分:1)

你是否错过了控制解释器的phps开启和关闭标签?

<?php
    ... your script code ...
?>

答案 1 :(得分:1)

以下查询

$ query = SELECT * FROM Username where username =&#39;&#34;。$ _ POST [&#34; username&#34;]。&#34;&#39;和密码=&#39;&#34;。$ _ POST [&#34;密码&#34;]。&#34;&#39; ;

也会回显您的查询,以便了解查询是否正在运行。

然后使用mysql_query($ query)或mysql_error();

答案 2 :(得分:1)

$row= mysql_fetch_array($query) or die (mysql_error());

应该是

$row= mysql_fetch_array($query,$con) or die (mysql_error());

答案 3 :(得分:0)

另外,如果您使用<? ?>检查php.ini中的short_open_tag = On:)