我创建了两个文件:
1. databaseconnect.php
2. checklogin.php
数据库连接.php:
class DatabaseConnection{
public $connected;
public $connection;
public function __construct($host="localhost", $username="abc", $password="def", $dbname="ghi") {
$tbl_name="login"; // Table name
$this->connection = new mysqli($host, $username, $password, $db_name);
/* check connection */
if ($mysqli->connect_error){
$this->connected=false;
}
else{
$this->connected=true;
}
}
}
检查登录.php:
<?php
include 'databaseconnect.php';
if($_POST){
$db = new DatabaseConnection;
if($db->connected){
//code flow enters here
$tbl_name = "login";
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = htmlspecialchars($myusername);
$mypassword = htmlspecialchars($mypassword);
$sql="SELECT user_id FROM $tbl_name WHERE user_id='$myusername' or primary_mail='$myusername'";
if($result=$db->connection->query($sql)){
//do something
//not coming in this loop
}
}
?>
我遇到错误
if($result=$db->connection->query($sql)){
可能是什么错误,使用它的正确语法是什么?
提前致谢