Home Backend Development PHP Tutorial Basic principles and examples of database field reuse_PHP tutorial

Basic principles and examples of database field reuse_PHP tutorial

Jul 13, 2016 pm 05:44 PM
one and binary Algebra Appear principle Basic Base Field number number database use use of Example express logic

1. Basics of logical algebra:

1. Numbers are represented in binary, and the only possible numbers are 0 and 1.

2. There are only three basic operations: "and", "or" and "not".

The AND operation is defined as: (Use & to express the AND operation)

 0 & 0 = 0

 0 & 1 = 0

 1 & 0 = 0

 1 & 1 = 1

It can be simply understood as: as long as there is a 0, the result is 0, similar to multiplication.

The OR operation is defined as: (Use | to represent the AND operation)

 0 | 0 = 0

 0 | 1 = 1

 1 | 0 = 1

 1 | 1 = 1

It can be simply understood as: as long as there is a 1, the result is 1, similar to addition.

2. Examples of logical operations:

 01111010101010101111111111111111 & 1100000 = 1100000

Generally can be understood as:

If you want to get the N-digit value of a number, you only need to AND the number with 2 raised to the N-1 power (mask).

3. Database field definition:

Take the data table binary_sample as an example:

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 field definition:

The data type of the status field is a 32-bit integer. In order to store as many attributes as possible, we define it as follows:

All the following “bits” are described in order from low to high (right to left).

0-2 digits indicate user registration status:

 000 means the new registration has not been approved

 001 means registration is approved

 010 indicates an advanced user

 011 means administrator

 100 means super administrator

 101 Reserved

 110 Reserved

 111 Mask

Gender of 3-5 users:

 000 indicates gender uncertainty

 001 indicates the gender is male

 010 indicates the gender is female

 011 Reserved

 100 Reserved

 101 Reserved

 110 Reserved

 111 Mask

If we want to query all male users:

Select * from binary_sample where status & b111000 = b001000;

If we want to query all administrator users:

select * from binary_sample where status & b111 = b011;

If we want to query all male administrator users:

Select * from binary_sample where status & b111111 = b001011;

If we want to query all non-newly registered and unapproved users then:

Select * from binary_sample where status & b111 != b000;

Fourth, use PHP program to perform such calculations:

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

If we want to query all male users:

 $status=GENDER_MALE;

 $mask=GENDER_MASK;

$sql="select * from binary_sample where status & ${mask} = ${status}";

If we want to query all administrator users:

$status=USER_MANAGE;

 $mask=USER_MASK;

$sql="select * from binary_sample where status & ${mask} = ${status}";

If we want to query all male administrator users:

$status=GENDER_MALE & USER_MANAGE;

$mask = GENDER_MASK & GENDER_MASK;

$sql="select * from binary_sample where status & ${mask} = ${status}";

If we want to query all non-newly registered and unapproved users then:

$status = USER_NEW;

 $mask = USER_MASK;

$sql="select * from binary_sample where status & ${mask} != ${status}";

By analogy, as long as the meaning of each value is defined, the query is basically settled.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478751.htmlTechArticle1. Basics of logical algebra: 1. Numbers are represented in binary, and the only possible numbers are 0 and 1. . 2. There are only three basic operations: "and", "or" and "not". AND operation definition...
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)

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Jun 25, 2024 pm 07:09 PM

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

Top 10 Global Digital Virtual Currency Trading Platform Ranking (2025 Authoritative Ranking) Top 10 Global Digital Virtual Currency Trading Platform Ranking (2025 Authoritative Ranking) Mar 06, 2025 pm 04:36 PM

In 2025, global digital virtual currency trading platforms are fiercely competitive. This article authoritatively releases the top ten digital virtual currency trading platforms in the world in 2025 based on indicators such as transaction volume, security, and user experience. OKX ranks first with its strong technical strength and global operation strategy, and Binance follows closely with high liquidity and low fees. Platforms such as Gate.io, Coinbase, and Kraken are at the forefront with their respective advantages. The list covers trading platforms such as Huobi, KuCoin, Bitfinex, Crypto.com and Gemini, each with its own characteristics, but investment should be cautious. To choose a platform, you need to consider factors such as security, liquidity, fees, user experience, currency selection and regulatory compliance, and invest rationally

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

PHP Database Connection Pitfalls: Avoid Common Mistakes and Misunderstandings PHP Database Connection Pitfalls: Avoid Common Mistakes and Misunderstandings Jun 05, 2024 pm 10:21 PM

To avoid PHP database connection errors, follow best practices: check for connection errors and match variable names with credentials. Use secure storage or environment variables to avoid hardcoding credentials. Close the connection after use to prevent SQL injection and use prepared statements or bound parameters.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

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.

Top 10 exchanges in the currency circle in 2025 latest digital currency app rankings Top 10 exchanges in the currency circle in 2025 latest digital currency app rankings Feb 27, 2025 pm 06:33 PM

Ranking of the top ten virtual currency trading platforms (latest in 2025): Binance: Global leader, high liquidity, and regulation has attracted attention. OKX: Large user base, supports multiple currencies, and provides leveraged trading. Gate.io: A senior exchange, with a variety of fiat currency payment methods, providing a variety of trading pairs and investment products. Bitget: Derivatives Exchange, high liquidity, low fees. Huobi: An old exchange that supports a variety of currencies and trading pairs. Coinbase: A well-known American exchange, strictly regulated. Phemex and so on.

Top 10 digital currency trading platforms The latest list of top 10 digital currency trading platforms Top 10 digital currency trading platforms The latest list of top 10 digital currency trading platforms Mar 17, 2025 pm 05:57 PM

Top 10 digital currency trading platforms: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi Global, 5. Kraken, 6. Coinbase, 7. KuCoin, 8. Bitfinex, 9. Crypto.com, 10. Gemini, these exchanges have their own characteristics, and users can choose the platform that suits them based on factors such as security, fees, currency selection, user interface and customer support.

See all articles