2013-12-13 107 views
1

我完全不熟悉数据库,我试图让我的登录页面检查数据库的用户名和密码。我得到这个错误: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'); 
+1

你想要mysql或sqlite吗?标题说sqlite,但我只看到mysql函数。 –

+1

btw,localhost和127.0.0.1是一回事。 –

+1

如果你正试图通过mysql接口使用sqlite数据库,那肯定是行不通的。 –

回答

0

连接到SQLite的

它不会看起来像您连接到SQLite数据库,因为你还没有指定的数据库文件的路径。试试这个小脚本,以更简洁的方式连接到你的数据库。

class Db { 

    private $config; 
    private $connection; 

    public function __construct() 
    { 
     $this->config = array(
      'path' => 'path/to/your/database.sqlite' 
     ); 

     $this->connect(); 
    } 

    private function connect() 
    { 
     $connectionQuery = 'sqlite:' .$this->config['path']; 

     try { 
      $this->connection = new PDO($connectionQuery); 
     } catch (PDOException $e) { 
      echo $e->getMessage(); 
     } 

     $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    } 

    public function getConnection() 
    { 
     return $this->connection; 
    } 

} 

$db = new DB(); 

数据库路径

更改此'路径/到/你/ database.sqlite'到任何你喜欢的路径。

检索连接处理

只需使用$ DB->的getConnection()访问它。

+0

为什么downvote?他正在寻求解决方案,以连接到他的sqlite数据库。 –

+0

不准确。他们感到困惑,你可能没有仔细阅读。他们有使用'mysqli'接口的数据库代码,他们希望将其转换为使用'sqlite'接口,但不知道如何。这些信息事实上是正确的,但与问题没有直接关系。您可以轻松修改它以直接回答问题。如果你这样做了,你可能会从我这里得到赞扬。 –

相关问题