我完全不熟悉數據庫,我試圖讓我的登錄頁面檢查數據庫的用戶名和密碼。我得到這個錯誤:PHP和SQLite沒有這樣的文件或目錄?
警告:mysqli_connect():(HY000/2002):在/Applications/XAMPP/xamppfiles/htdocs/checklogin.php在線11 沒有這樣的文件或目錄無法連接
運行XAMPP,我嘗試修改php.ini文件,並嘗試連接到127.0.0.1而不是本地主機。我不確定它爲什麼這樣做。另外我的test.db文件應該放在哪裏?在與index.html相同的目錄中?
<?php
$host="localhost"; // Host name
$username="test"; // Mysql username
$password="test"; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysqli_connect("$host", "$username", "$password")or die("cannot connect");
mysqli_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysqli_real_escape_string($myusername);
$mypassword = mysqli_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysqli_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
這項工作是否也適用?
function getConnected($host,$username,$password,$db_name)
{
$mysqli = new mysqli($host, $username, $password, $db_name);
if($mysqli->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $mysqli;
}
$mysqli = getConnected('localhost','user','password','database');
你想要mysql或sqlite嗎?標題說sqlite,但我只看到mysql函數。 –
btw,localhost和127.0.0.1是一回事。 –
如果你正試圖通過mysql接口使用sqlite數據庫,那肯定是行不通的。 –