php 数据库字段复用的基本原理与示例
一、逻辑代数基础:
1,数字用二进制表示,所有可能出现的数只有0和1两个。
2,基本运算只有“与”、“或”、“非”三种。
与运算定义为:(用 & 表示与运算)
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
可以简单理解为:只要有一个0,结果就是0,和乘法类似。
或运算定义为:(用 表示与运算)
0 0 = 0
0 1 = 1
1 0 = 1
1 1 = 1
可以简单理解为:只要有一个1,结果就是1,和加法类似。
二、逻辑运算示例:
01111010101010101111111111111111 & 1100000 = 1100000
一般可以理解为:
如果要获取一个数字某N位的数值,只需要将这个数字与2的N-1次方(掩码)进行与运算即可。
三、数据库字段定义:
以数据表 binary_sample为例:
create table binary_sample(
uid int unsigned not null,
status int unsigned not null default 0,
primary key(uid),
key i_s(status)
)engine=innodb;
status字段定义:
status字段数据类型为32bit的整数,为了尽可能的存储多个属性,我们将其进行如下定义:
以下所有“位”的描述顺序按照从低到高(从右到左)顺序表示。
0-2位表示用户注册状态:
000 表示新注册未被批准
001 表示注册被批准
010 表示位高级用户
011 表示管理员
100 表示超级管理员
101 保留
110 保留
111 掩码
3-5位用户性别:
000 表示性别不确定
001 表示性别为男
010 表示性别为女
011 保留
100 保留
101 保留
110 保留
111 掩码
如果我们要查询所有 男用户 则:
select * from binary_sample where status & b'111000' = b'001000';
如果我们要查询所有 管理员用户 则:
select * from binary_sample where status & b'111' = b'011';
如果我们要查询所有 男管理员用户 则:
select * from binary_sample where status & b'111111' = b'001011';
如果我们要查询所有 非 新注册未被批准用户 则:
select * from binary_sample where status & b'111' != b'000';
四,使用PHP程序进行此类计算:
define("USER_NEW",0);//000
define("USER_NORMAL",1);//001
define("USER_ADVANCE",2);//010
define("USER_MANAGE",3);//011
define("USER_SUPER",4);//100
define("USER_MASK",7);//111
define("GENDER_UNKNOWN",0);// 000000
define("GENDER_MALE",8);// 001000
define("GENDER_FEMALE",9);// 010000
define("GENDER_MASK",56);// 111000
如果我们要查询所有 男用户 则:
$status=GENDER_MALE;
$mask=GENDER_MASK;
$sql="select * from binary_sample where status & ${mask}' = ${status}";
如果我们要查询所有 管理员用户 则:
$status=USER_MANAGE;
$mask=USER_MASK;
$sql="select * from binary_sample where status & ${mask}' = ${status}";
如果我们要查询所有 男管理员用户 则:
$status=GENDER_MALE & USER_MANAGE;
$mask = GENDER_MASK & GENDER_MASK;
$sql="select * from binary_sample where status & ${mask}' = ${status}";
如果我们要查询所有 非 新注册未被批准用户 则:
$status = USER_NEW;
$mask = USER_MASK;
$sql="select * from binary_sample where status & ${mask} != ${status}";
依此类推,只要定义好每个值的含义,查询基本上就定了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.
