What are the common read and write operations in PHP programming?
PHP is a highly flexible programming language that is widely used in the field of web development. In PHP programming, reading and writing operations are two very basic operations. This article will introduce common read and write operations in PHP.
1. Reading operation
The file reading operation is to read the data in the file and store it in a variable middle. Several built-in functions are provided in PHP to implement file reading operations:
(1) file_get_contents()
The file_get_contents() function can read the contents of the entire file and store it as a String. This function can accept a file name or URL address as a parameter, and also supports file streams and HTTP protocols. The following is a simple usage example:
$file_data = file_get_contents('data.txt'); echo $file_data;
(2) fread()
fread() function can read data of a specified length and store it as a string. This function needs to pass in two parameters: file pointer and read length. The following is a simple usage example:
$fp = fopen('data.txt', 'r'); $file_data = fread($fp, 1024); echo $file_data; fclose($fp);
The database read operation is to read data from the database and store it in a variable. PHP provides several built-in functions to implement database reading operations:
(1) mysqli_fetch_array()
The mysqli_fetch_array() function can obtain a row of data from the result set and store it as a array. This function needs to pass in two parameters: the result set and the data type. The following is a simple usage example:
$conn = mysqli_connect('localhost', 'root', 'password', 'test'); $result = mysqli_query($conn, 'SELECT * FROM users'); while($row = mysqli_fetch_array($result)) { echo $row['id'] . ' ' . $row['username'] . '<br>'; } mysqli_close($conn);
(2) PDOStatement::fetch()
The PDOStatement::fetch() function can obtain a row of data from the result set and store it as a array. This function needs to pass in a parameter to obtain the data type. The following is a simple usage example:
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', 'password'); $sql = 'SELECT * FROM users'; $stmt = $pdo->query($sql); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo $row['id'] . ' ' . $row['username'] . '<br>'; } $pdo = null;
2. Writing operation
The file writing operation is to write data into the file. Several built-in functions are provided in PHP to implement file writing operations:
(1) file_put_contents()
The file_put_contents() function can write a string to a file, if the file does not exist create. This function can accept three parameters, namely the file name, the data to be written and the writing mode. The following is a simple usage example:
$file_data = 'Hello, World!'; file_put_contents('data.txt', $file_data, FILE_APPEND);
(2) fwrite()
The fwrite() function can write a string of specified length into a file. This function needs to pass in three parameters: the file pointer, the data to be written, and the specified length. The following is a simple usage example:
$fp = fopen('data.txt', 'a'); $file_data = 'Hello, World!'; fwrite($fp, $file_data); fclose($fp);
The database writing operation is to write data into the database. PHP provides several built-in functions to implement database writing operations:
(1) mysqli_query()
The mysqli_query() function can execute a SQL statement and return the execution result. This function needs to pass in two parameters: database connection and SQL statement. The following is a simple usage example:
$conn = mysqli_connect('localhost', 'root', 'password', 'test'); $sql = "INSERT INTO users (username, password) VALUES ('test', '123456')"; $result = mysqli_query($conn, $sql); mysqli_close($conn);
(2) PDOStatement::execute()
The PDOStatement::execute() function can execute a prepared SQL statement and return the execution result. This function requires one parameter to be passed in as a binding parameter. The following is a simple usage example:
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', 'password'); $sql = "INSERT INTO users (username, password) VALUES (?, ?)"; $stmt = $pdo->prepare($sql); $stmt->execute(['test', '123456']); $pdo = null;
Reading and writing operations are very basic and commonly used operations in PHP programming. Mastering these operations will make it easier to operate files and databases and perform data processing.
The above is the detailed content of What are the common read and write operations in PHP programming?. For more information, please follow other related articles on the PHP Chinese website!