Table of Contents
Reply content:
Home Backend Development PHP Tutorial If I want to build a website with a very small number of users, how should I choose data storage?

If I want to build a website with a very small number of users, how should I choose data storage?

Aug 04, 2016 am 09:19 AM
php sql data database

I want to build a website with a very small number of users. The number of users is from a few to more than a dozen. This number of users is really very small.
The backend uses php, and now I want to know how to store data.

Because the number of users is very small, the host configuration will also be very low. Using mysql feels overkill, and may affect performance because the host configuration is too low.

I have considered saving it directly into a file (the data has a relatively simple structure, and json can handle it), but if there is a related query (such as mysql's join), then you have to use PHP to do the related query yourself. It feels like Some trouble.

Is there any better way? Like a super lightweight database?

PS: You don’t need redis because the host configuration is low and the memory is definitely tight. Companies like mysql and redis that work hard to produce miracles cannot exert their strength in such a sloppy environment.

Reply content:

I want to build a website with a very small number of users. The number of users is from a few to more than a dozen. This number of users is really very small.
The backend uses php, and now I want to know how to store data.

Because the number of users is very small, the host configuration will also be very low. Using mysql feels overkill, and may affect performance because the host configuration is too low.

I have considered saving it directly into a file (the data has a relatively simple structure, and json can handle it), but if there is a related query (such as mysql's join), then you have to use PHP to do the related query yourself. It feels like Some trouble.

Is there any better way? Like an ultra-lightweight database?

PS: You don’t need redis because the host configuration is low and the memory is definitely tight. Companies like mysql and redis that work hard to produce miracles cannot exert their strength in such a sloppy environment.

Use SQLite, or simply file storage. If file storage is used, the data files should be placed outside the webroot.

The correct answer upstairs, sqlite. It is lightweight and does not require services, just files. The client of the project that I participated in the research and development before used sqlite

It’s still mysql. The efficiency of file reading and writing is lower than using mysql, and it will also cause additional difficulties when you develop. I suggest you try it first. Will the host configuration be lower than that of the Raspberry Pi? Of course, if the host configuration is already low, there is no way to install Windows.

If you are developing using ORM, choosing a database in the early stage is not something you have to consider.
If you are writing sql development, then you have to consider that if the database is changed in the future, the sql you write may not be compatible.

No matter how low the host configuration is, it is not as exaggerated as you think. Mysql and postgres can definitely run

SQLite is recommended, PHP comes with its own extensions. Although the functions and data types are much simpler than MySQL, the commonly used ones are fine, and the performance is also very good.

If you use SQLite, it is best not to place the data file in the root directory of the website, otherwise people will download it if they know the URL.
Of course you can configure Apache/Nginx to deny requests to access specified files, but it is still a hidden danger after all,
So still Put it outside the root directory of the website.
In addition, if you use SQLite, you have to write SQL language and use the PDO set of functions to operate:

<code><?php

function db() {
    static $db;
    if ($db) {
        return $db;
    } else {
        try {
            $db = new PDO('sqlite:'.$_SERVER['DOCUMENT_ROOT'].'/../data.db3');
        } catch (PDOException $e) {
            echo $e->getMessage();
            exit();
        }
        return $db;
    }
}

function insert($title = '', $content = '') {
    global $app;
    $db = db();
    $stmt = $db->prepare('INSERT INTO posts (post_title, post_content) VALUES (?, ?)');
    $stmt->bindParam(1, $title,   PDO::PARAM_STR);
    $stmt->bindParam(2, $content, PDO::PARAM_STR);
    $stmt->execute();
    return ($stmt->rowCount() !== 0) ? 
        array(true,  'lastInsertId' => $db->lastInsertId()) : 
        array(false, 'lastInsertId' => $db->lastInsertId());
}

function select($id = '') {
    global $app;
    $db = db();
    if (!empty($id)) {
        return $db->query('SELECT * FROM posts WHERE id = '.intval($id))->fetchAll(PDO::FETCH_ASSOC);
    } else {
        return $db->query('SELECT * FROM posts')->fetchAll(PDO::FETCH_ASSOC);
    }
}

function select_v2($id = '') {
    global $app;
    $db = db();
    if (!empty($id)) {
        $stmt = $db->prepare('SELECT * FROM posts WHERE id = ?');
        $stmt->bindParam(1, $id, PDO::PARAM_INT);
    } else {
        $stmt = $db->prepare('SELECT * FROM posts');
    }
    $stmt->execute();
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

function update($id, $title = '', $content = '') {
    global $app;
    $db = db();
    
    //echo PDO::ATTR_AUTOCOMMIT; //返回0可见PDO默认禁用自动提交事务.
    //echo $db->getAttribute(PDO::ATTR_AUTOCOMMIT); exit(); //返回1可见MySQL默认会自动提交事务.
    
    //SQLite不支持设置PDO::ATTR_AUTOCOMMIT:
    //SQLite: Uncaught exception 'PDOException' with message 'The auto-commit mode cannot be changed for this driver'
    //$db->setAttribute(PDO::ATTR_AUTOCOMMIT, false);
    $db->beginTransaction();
    $stmt = $db->prepare('UPDATE posts SET post_title = ?, post_content = ? WHERE id = ?');
    
    $stmt->execute(array($title,$content,$id)); //所有值视作PDO::PARAM_STR处理
    //$stmt->execute(array(':title' => $title,':content' => $content,':id' => $id));
    //$stmt->bind_param('ssi', $title, $content, $id); //对比mysqli
    
    echo 'sleep(3);'."\n";
    sleep(3);
    
    $db->commit();
    //$db->setAttribute(PDO::ATTR_AUTOCOMMIT, true); //commit提交事务后autocommit记得重新设为true
    return ($stmt->rowCount() !== 0) ? true : false;
}

function delete($id) {
    global $app;
    $db = db();
    return ($db->query('DELETE FROM posts WHERE id = '.intval($id))->rowCount() !== 0) ? true : false;
}

function delete_v2($id) {
    global $app;
    $db = db();
    $stmt = $db->prepare('DELETE FROM posts WHERE id = ?');
    $stmt->bindParam(1, $id, PDO::PARAM_INT);
    $stmt->execute();
    return ($stmt->rowCount() !== 0) ? true : false;
}

header('Content-Type: text/plain; charset=utf-8');

$sqlite = "CREATE TABLE IF NOT EXISTS posts (
    id           INTEGER PRIMARY KEY,
    post_title   VARCHAR(255) NOT NULL,
    post_content TEXT         NOT NULL
)";

db()->query('DROP TABLE IF EXISTS posts;') or exit();
db()->query($sqlite) or exit();

//并发时,SQLite在insert时因为库文件被其他请求锁住而导致阻塞
echo "var_export(insert('标题1', '内容1'));\n";
var_export(insert('标题1', '内容1'));
echo "\n\n";

echo "var_export(insert('标题2', '内容2'));\n";
var_export(insert('标题2', '内容2'));
echo "\n\n";

echo "var_export(select());\n";
var_export(select());
echo "\n\n";

echo "var_export(update(2, '标题2_更新','内容2_更新'));\n";
var_export(update(2, '标题2_更新','内容2_更新'));
echo "\n\n";

echo "var_export(select(2));\n";
var_export(select(2));
echo "\n\n";

echo "var_export(delete(2));\n";
var_export(delete(2));
echo "\n\n";

echo "var_export(select());\n";
var_export(select());
echo "\n\n";</code>
Copy after login

I wonder if you have such an experience, PHP is actually an array-oriented programming language.
In fact, you can consider directly exporting PHP arrays into files to store data.

<code><?php
header('Content-Type: text/plain; charset=utf-8');
$file = __DIR__.'/data.php'; //数据文件,别人直接URL访问也下载不了
if(!file_exists($file)) {
    file_put_contents($file, '<?php return array();'); //file_put_contents($file, '');
}
$fp = fopen($file, 'r+'); //读写方式打开,将文件指针指向文件头
if(flock($fp, LOCK_EX)) { //阻塞到获取排它锁
    //锁定数据文件后再进行读写
    $arr = require $file; //$arr = unserialize(file_get_contents($file));
    $arr[] = date('Y-m-d H:i:s');
    ftruncate($fp, 0); //清空文件
    fwrite($fp, '<?php return '.var_export($arr, true).';'); //fwrite($fp, serialize($arr));
    fflush($fp); //在释放锁之前刷新输出
    //sleep(10); //睡眠10秒,在此期间其他工作进程的请求将被阻塞
    flock($fp, LOCK_UN); //释放锁定
    echo file_get_contents($file)."\n";
}
fclose($fp);</code>
Copy after login

The comments also provide methods to serialize/unserialize serialized storage like PHP sessions.
It is worth mentioning that the performance of serialize/unserialize is much better than var_export/require.
But the advantage of var_export/require is that it is not afraid of others For direct access, you can put the file in the root directory and it is more readable.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1660
14
PHP Tutorial
1260
29
C# Tutorial
1233
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

See all articles