Home Backend Development PHP Tutorial Detailed explanation of commonly used functions in PHP database

Detailed explanation of commonly used functions in PHP database

Mar 09, 2018 am 10:45 AM
php database Detailed explanation

This article mainly shares with you detailed explanations of commonly used functions in PHP databases, hoping to help everyone.
Array function
extract()
can convert subscripts in associative arrays into variables for use

$stu = array('id'=>1,'name'=>Loser);
那么,id和name就可以作为变量使用了$id,$name
key()
从关联数组中取得键名
current()
返回数组中的当前单元
next()
将数组中的内部指针向前移动一位
end()
将指针指向最后一个元素
prev()
将数组中的内部指针向后移动一位
each()
返回数组中当前的键/值对并将数组指针向前移动一步,如果内部指针
穿越了数组的末尾返回false
list()
将数组中的值赋给变量
$arr = array('tom','david');
list($one, $two) = $stu;
range(A, B)
生成一个A-B的数组,倒序也可以
{{array_merge()
数组合并
array_merge(range(0,1),range('a','z'),range('A','Z'))
summary:数组合并时下标冲突,按照以下规则解决
1.如果是字符串下标,后面的元素会将前面的元素覆盖
2.如果是整数下标,重新建立下标(按照0,1,2,3的顺序来重新定义)
array_merge_recursive() 
数组合并,与array_merge没有太多的不同
最大的区别就是当有键名重复时,array_merge()会把相同键名的元素进行覆盖
而array_merge_recursive()会把相同键名的元素组合成一个新数组
}}
{{array_rand(数组,随机取出元素的个数)
随机取出数组内的元素,返回随机数组的下标
如果随机取出一个,返回整型下标
如果随机取出多个,返回整型下标数组array(A, B, C)
}}
{{shuffle(数组)
打乱数组
}}
{{
数组键值函数
[[
array_keys($array)返回数组中所有的键名
array_values($array)返回数组中的所有值
这两个返回值都是以数组的形式
]]
[[
array_combine($array1, $array2)
把两个数组进行结合,第一个数组的值做键名,第二个数组的值做值
如果键数组和值数组个数不一致,会报错
]]
[[
in_array(值, $array)
查看值是否在数组$array中
如果存在,返回true,如果不存在,返回false
区分大小写
不区分数据类型
]]
[[
array_key_exists(键, $array)
查看数组中是否包含某一个键值
类型同上
]]
}}
{{
}}
 ******************************************************/
 /******************************************************
 普通函数
sleep(参数)
延时执行
参数为秒数
 ******************************************************/
/******************************************************
数据库函数
mysql_connect('localhost','username','pwd')
连接数据库
第一个参数表示连接地址
第二个参数表示用户名
第三个参数表示密码
{{
连接数据库
mysql_connect('主机地址:[端口号]','用户名','密码')
选择数据库
mysql_query('use 数据库名')
mysql_select_db(数据库名)
设置执行环境
mysql_query('set names utf8')
}}
{{
查看数据库名称(匹配查找)
show databases like "%***%";
%是指匹配,在前面就是匹配从名字前面开始匹配,在后面就是从
名字后面开始匹配
}}
{{
创建数据库
create database ***;
}}
{{
#增加用户
#格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by '密码'
}}
{{
增删改查(语句)
#创建数据库
create database tb_name;
#删除数据库
drop database tb_name;
#使用该数据库
use tb_name;
#显示数据库中的表
show tables;
#先判断表是否存在,存在先删除
drop table if exists student;
#创建表
create table student(
id int auto_increment primary key,
name varchar(50),
sex varchar(20),
date varchar(50),
content varchar(100)
)default charset=utf8;
#删除表
drop table student;
#查看表的结构
describe student;  #可以简写为desc student;
#插入数据
insert into tb_name values('***','***','***');
#查询表中的数据
select * from tb_name;
select id,name from tb_name;
#修改某一条数据
update tb_name set sex='男' where id=4;
#删除数据
delete from student where id=5;
#蠕虫复制//让数据库成倍数增长
//插入的是重复的数据,没有很大的业务意义,主要用来测试数据表的压力或索引的效率
insert into tb_name (field) select field from tb_name;
}}
判断变量是否为空   合集
isset()  判断变量是否设置,并且设置的值不为空  
如果返回值为被创建,那么返回值为true
empty()  判断变量是否为空
判断数据类型   合集
is_numeric()判断是否是数字或者数字字符串
is_array()判断是否是数组
is_string()判断是否是字符串
is_int()判断是否是整形
is_double()判断是否是浮点型 
is_bool()判断是否是bool类型
字符串函数
is_string()判断是否是字符串
strlen($num)判断变量(字符串)的长度
implode(分隔符, $array)把数组内的值拼接成字符串
explode(分隔符, $array)把字符串按照分隔符切割成数组
[[
字符串可以理解为字符的集合(数组),所以可以通过[]来访问,不能操作中文
]]
[[
strlen()返回字符串的长度
注意:在gbk下一个汉字占两个字节,在utf-8下一个汉字占三个字节
]]
[[
去除空格函数
trim(),ltrim(),rtrim()
第一个去除前后空格,第二个去除左边空格,第三个去除右边空格
]]
[[
strpos(字符串, 'str')
查找str在字符串中的位置,从左开始,查找第一次出现的位置
strrpos(字符串, 'str')
同上,区别就是从有开始查找第一次出现的位置,但是返回的值也是
从右边开始数的
]]
[[
str_repeat($str, num)
将字符串重复几次
$word = str_repeat('你好',3);
]]
[[
ord() 获取字符的ASCII码
chr()将ASCII码转成字符
]]
[[
Copy after login


Intercept string
substr(string, starting position , intercept length)
If the starting position is a negative number, then count from -1, if it is an integer, start counting from 0
If the intercept length is empty , then the default is to intercept until the end
If the interception length is a negative number, it means that the following characters will not be intercepted
strchr(string, 'str')
Intercept from the string according to str
$str = 'abccba';
echo strchr($str, 'b'); result: bccba
strrchr(string, 'str')
The rules are the same as above, the difference is that starting from the right
str_replace(' strone', 'strtwo', $str) Replace string
In $str, replace strone with strtwo
Replace multiple strings at one time
In the form of an array
$str = 'Tomorrow after tomorrow, there are so many tomorrows';
str_replace(array('tomorrow',' Complex'),array('1','2'),$str);
result: "12 tomorrows, so many 1"
Note: If If there is no one-to-one correspondence, all
elements in the first array will be replaced with the second parameter
str_split($array, num)
Split the string into an array
num refers to how many values ​​there are in each array element
str_pad(string, length , 'Supplementary')
String filling, fill the string to the specified length, and use "supplementary value" to fill if it is insufficient
Add at the end STR_PAD_LEFT will start from the left
Add STR_PAD_BOTH at the end and it will start from both sides
String case conversion
strtolower()
Convert all strings to lowercase
strtoupper()
Convert all strings to uppercase
nl2br()
Convert the newline of the string to

htmlspecialchars()
Convert the string to an entity
]]
Sort function
sort()
Sort the elements in the array alphabetically Sort in ascending order
rsort()
Sort the elements in the array in descending alphabetical order
Object-oriented

{{
增加属性
直接用$str->新属性名 = 'str';
}}
{{
删除属性
unset($str->属性名)
}}
{{
判断属性是否存在
isset($str->属性名)
}}
{{
instanceof
用来判断变量是否属于某一个数据类型
class Student{}
$stu = new Student;
var_dump($stu instanceof Student);
}}
Copy after login


The above is the detailed content of Detailed explanation of commonly used functions in PHP database. For more information, please follow other related articles on the PHP Chinese website!

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
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

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 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 and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

See all articles