【问题标题】:how to connect php to SQLite3 not in same directory如何将php连接到不在同一目录中的SQLite3
【发布时间】:2018-03-23 10:08:56
【问题描述】:

我将 SQLite3 与 php 一起使用,当我使用同一文件夹中的所有 PHP 文件时可能工作。 但我想使用文件夹包括数据库和 API 脚本,另一个文件夹包括 php 文件来插入数据和最后一个文件夹来从 db 中选择数据,如下所示:

第一个文件夹incDB包括:

testCreate.php 文件

<?php
class mySqlite extends SQLite3{
    function __construct(){
        $this->open('test.db');
    }
}
$db = new mySqlite();
$db->exec("CREATE TABLE IF NOT EXISTS `test` (
    `id`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `name`  TEXT NOT NULL);"
    );
?>

testAPI.php文件

<?php
require_once('testCreate.php');

function insert($name){
    global $db;
    $stmt = $db->exec("INSERT INTO `test`(`name`) VALUES ('$name')");
    if($stmt){
        return true;
    }else{
        return false;
    }
}

function select(){
    global $db;
    if($stmt = $db->prepare('SELECT id,name FROM test ORDER BY `id` DESC '))
    {
        $result = $stmt->execute();
        $names=array();
        while($arr=$result->fetchArray(SQLITE3_ASSOC))
        {
         $names[]=$arr;
        }
        return $names;
    }
}
?>

第二个文件夹cms包括:

插入.php

<?php
if(isset($_POST['new_menu']) && !empty($_POST['new_menu'])){
    require_once('../incDB/testAPI.php');
    $qry = insert($_POST['new_menu']);
    if($qry) echo 'ok';
    else echo 'no';
}
?>
<form method='post'>
    <input type="text" name="new_menu">
    <button type="submit">ADD</button>
</form>

第三个​​文件夹内容包括:

select.php

<?php
require_once('../incDB/testAPI.php');
$rows = select();
?>
<table>
    <thead>
        <td style="border: solid 1px #000">id</td>
        <td style="border: solid 1px #000">name</td>
    </thead>
    <?php
       foreach($rows as $row){
    ?>
    <tr>
        <td style="border: dashed 1px #000"><?php echo $row['id']; ?></td>
        <td style="border: dashed 1px #000"><?php echo $row['name']; ?></td>
    </tr>
    <?php } ?>
</table>

当我在浏览器中运行 insert.php 或 select.php 文件时,我在文件的同一文件夹中找到了数据库,我想在 incDB 文件夹中的数据库中插入和选择数据。

=============== 更新

我将 testAPI.php 文件更新到这个

<?php
//require_once('testCreate.php');
class mySqlite extends SQLite3{
    function __construct(){
        $this->open('test.db');
    }
}
$db = new mySqlite('../incDB/test.db');
$db->exec("CREATE TABLE IF NOT EXISTS `test` (
`id`    INTEGER PRIMARY KEY AUTOINCREMENT,
`name`  TEXT NOT NULL);"
);

function insert($name){
    global $db;
    $stmt = $db->exec("INSERT INTO `test`(`name`) VALUES ('$name')");
    if($stmt){
        return true;
    }else{
        return false;
    }
}

function select(){
    global $db;
    if($stmt = $db->prepare('SELECT id,name FROM test ORDER BY `id` DESC '))
    {
        $result = $stmt->execute();
        $names=array();
        while($arr=$result->fetchArray(SQLITE3_ASSOC))
        {
         $names[]=$arr;
        }
        return $names;
    }
}
?>

仍然是同样的问题。在 insert.php 和 select.php 的同一文件夹中创建数据库

【问题讨论】:

  • test.db 使用绝对路径名。
  • 同一文件夹中的 testAPI.php 、 test.db 和 testCreate.php 文件。如何使用绝对路径名?在这种情况下,脚本是在 insert.php 文件的同一文件夹和 select.php 文件的同一文件夹中制作 test.db
  • 问题说他们不在在同一个文件夹中。
  • select.php 和 insert.php 不在同一个文件夹中,而 API 在 test.db 的同一个文件夹中
  • 问题是相对路径名是相对于原始脚本解释的,而不是 API 脚本。如果可以从不同的文件夹使用 API,则不能在 API 中使用相对路径名。您需要使用数据库的完整路径名。

标签: php sqlite


【解决方案1】:

我发现了错误

testCreate.php 中打开函数需要完整路径

答案是

<?php
class mySqlite extends SQLite3{
    function __construct(){
        $this->open('../incDB/test.db'); /// in this line mistake
    }
}
$db = new mySqlite();
$db->exec("CREATE TABLE IF NOT EXISTS `test` (
    `id`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `name`  TEXT NOT NULL);"
    );
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 2012-09-05
    • 2011-07-23
    相关资源
    最近更新 更多