There are several ways to modify the database in PHP
In the PHP development process, we need to connect to the database to develop dynamics There are usually four ways to connect to a database on a website: Mysql-connect(),Mysqli,PDO,ADODB, we can use this How to connect to the database in 4 to modify the database will be introduced one by one next.
Mysql_connect
This is the first database connection method we encountered when we first started learning PHP. The connection result can be returned through the mysql_connect() function, then a MySQL connection identifier, returns FALSE if failed. Therefore, subsequent database modification operations can be performed.
<?php $con=mysql_connect("localhost" ,"root","password") if($con){ mysql_select_db("db_name",$con); $sql="select * from table_name where id=1"; $result=mysql_query($sql); while($row=mysql_fetch_row($result)){ echo "$row"; } }else{ die("无法连接数据库".mysql_error()); } mysql_close($con); ?>
This connection method is a short connection, not a long connection. If the connection is long, use mysql_pconnectct()
Mysqli
This is a The object-oriented database connection method requires instantiating an object before connecting, and then performing database operations through this object.
<?php $con=new mysqli("localhost","root","password","db_name"); if(!mysqli_connect_error()){ $sql="select * from table_name where id=1"; $result=$con->query($sql); while($row=$result->fetch_row($result)){ echo "$row"; } }else{ die("无法连接数据库".mysql_error()); }
mysqli is a long connection method and is more secure than mysql_connect
PDO
What is pdo, pdo is added to the php5 center The way to connect to the database,
<?php $pdo=new PDO('mysql:host=localhost;dbname=db_name',"root", $sql="select * from table_name where id=1"; "password"); try{ $result=$pdo->query($sql); foreach($result as $k){ print_r($k); } }catch(PDOException $e){ echo $e->getMessage(); } ?>
ADODB
No matter what the back-end database is, the way to access the database is the same, and developers don’t have to settle for a certain set of Database, you must learn another set of different access methods, which greatly reduces the knowledge burden of developers. Past knowledge can still be used in the future
require_once './adodb5/adodb.inc.php'; $conn = &ADONewConnection('mysql'); $conn->connect('localhost','root','','test'); $conn->Execute("set names utf8"); $res = $conn->Execute("select * from user"); if (!$res){ echo $conn->ErrorMsg(); }else{ var_dump($res); }
For more PHP related knowledge, please visitPHP中文网!
The above is the detailed content of There are several ways to modify the database in PHP. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

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