尝试连接 php 中的数据库,但出现语法错误
Trying to connect a database in php, but get syntax error
我正在尝试使用我在 PHPMyAdmin 中创建的数据库制作登录页面,但每次我 运行 它时,我都会在 login.php 的第 2 行收到语法错误,但我没有知道为什么它不起作用。我确定我输入的代码是正确的。当我 运行 它时,我得到一个错误:
解析错误:语法错误,第 2 行 login.php 中的意外“:”
login.php
<?php
$dsn = 'mysql:host=localhost;dbname=dbase';
$username = 'user1';
$password = 'pass';
$error = "";
try{
$db = new PDO ($dsn, $username, $password);
}
catch (PDOException $e){
$error = $e->getMessage();
}
// end of database connect
$user = $_POST['user'];
$pass = $_POST['pass'];
$query = 'SELECT * FROM users WHERE user = :user AND pass = :pass';
$statement = $db->prepare($query);
$statement->bindValue(':user', $user);
$statement->bindValue(':pass', $pass);
$statement->execute();
$valid = ($statement->rowCount() == 1);
$result = $statement->fetch();
$statement->closeCursor();
if ($valid){
$greeting = $result['email'];
$permit = ", login success";
}
else{
$greeting = $user;
$permit = ", invalid credentials";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Scheduler</title>
</head>
<body>
<h1><?php echo $greeting.$permit; ?></h1>
</body>
</html>
loginpage.php
<html>
<head>
<title>Scheduler</title>
</head>
<body>
<form action = 'login.php' method = "post">
<h1>EZ-Scheduler</h1>
<h2>Please login to view rosters</h2>
<table>
<tr>
<td>Username:</td>
<td><input name = "user" type = "text" size = "25"></td>
</tr>
<tr>
</tr>
<tr>
<td>Password:</td>
<td><input name = "pass" type = "password" size = "25"></td>
</tr>
</table>
<p><input type = "submit" name = "button" value = "Login"></p>
</table>
</form>
</body>
</html>
试试这个东西看看
<?php
$username = 'user1';
$password = 'pass';
$error = "";
try{
$db = new PDO ('mysql:host=localhost;dbname=dbase', $username, $password);
}
catch (PDOException $e){
$error = $e->getMessage();
}
// end of database connect
我正在尝试使用我在 PHPMyAdmin 中创建的数据库制作登录页面,但每次我 运行 它时,我都会在 login.php 的第 2 行收到语法错误,但我没有知道为什么它不起作用。我确定我输入的代码是正确的。当我 运行 它时,我得到一个错误:
解析错误:语法错误,第 2 行 login.php 中的意外“:”
login.php
<?php
$dsn = 'mysql:host=localhost;dbname=dbase';
$username = 'user1';
$password = 'pass';
$error = "";
try{
$db = new PDO ($dsn, $username, $password);
}
catch (PDOException $e){
$error = $e->getMessage();
}
// end of database connect
$user = $_POST['user'];
$pass = $_POST['pass'];
$query = 'SELECT * FROM users WHERE user = :user AND pass = :pass';
$statement = $db->prepare($query);
$statement->bindValue(':user', $user);
$statement->bindValue(':pass', $pass);
$statement->execute();
$valid = ($statement->rowCount() == 1);
$result = $statement->fetch();
$statement->closeCursor();
if ($valid){
$greeting = $result['email'];
$permit = ", login success";
}
else{
$greeting = $user;
$permit = ", invalid credentials";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Scheduler</title>
</head>
<body>
<h1><?php echo $greeting.$permit; ?></h1>
</body>
</html>
loginpage.php
<html>
<head>
<title>Scheduler</title>
</head>
<body>
<form action = 'login.php' method = "post">
<h1>EZ-Scheduler</h1>
<h2>Please login to view rosters</h2>
<table>
<tr>
<td>Username:</td>
<td><input name = "user" type = "text" size = "25"></td>
</tr>
<tr>
</tr>
<tr>
<td>Password:</td>
<td><input name = "pass" type = "password" size = "25"></td>
</tr>
</table>
<p><input type = "submit" name = "button" value = "Login"></p>
</table>
</form>
</body>
</html>
试试这个东西看看
<?php
$username = 'user1';
$password = 'pass';
$error = "";
try{
$db = new PDO ('mysql:host=localhost;dbname=dbase', $username, $password);
}
catch (PDOException $e){
$error = $e->getMessage();
}
// end of database connect