Home  >  Article  >  Backend Development  >  How to use PHP to implement data storage and retrieval functions

How to use PHP to implement data storage and retrieval functions

WBOY
WBOYOriginal
2023-06-25 12:22:051027browse

PHP is a popular server-side programming language that can easily interact with database systems and is the first choice in the field of web application development. This article will explore how to use PHP to implement data storage and retrieval functions.

1. Use PHP to connect to the database

Use the mysqli function in PHP to connect to the database, and you need to enter the user name, password and database name. The following is a sample code to connect to the database:

<?php 
$hostname = "localhost"; 
$username = "root"; 
$password = ""; 
$databaseName = "my_database"; 

// 创建数据库连接 
$connect = mysqli_connect($hostname, $username, $password, $databaseName); 

// 检查连接是否成功 
if (!$connect) { 
  die("连接失败: " . mysqli_connect_error()); 
} 
echo "连接成功"; 
?>

2. Use PHP to implement data storage function

  1. Insert data

Use PHP to insert data into the database table To insert data, you need to use the mysqli_query() function. The following is a sample code that stores user information into a database table:

<?php 
$hostname = "localhost"; 
$username = "root"; 
$password = ""; 
$databaseName = "my_database"; 

// 创建数据库连接 
$connect = mysqli_connect($hostname, $username, $password, $databaseName); 

// 检查连接是否成功 
if (!$connect) { 
  die("连接失败: " . mysqli_connect_error()); 
} 

// 准备要插入的数据 
$firstname = "John"; 
$lastname = "Doe"; 
$email = "john.doe@example.com"; 

// 向数据库表中插入数据 
$sql = "INSERT INTO users (firstname, lastname, email) VALUES ('$firstname', '$lastname', '$email')"; 

if (mysqli_query($connect, $sql)) { 
  echo "数据成功插入到数据库表中"; 
} else { 
  echo "数据插入失败: " . mysqli_error($connect); 
} 

// 关闭数据库连接 
mysqli_close($connect); 
?>
  1. Update data

Using PHP to update data in the database table, you need to use mysqli_query() function. Here is a sample code that updates the username with user ID 1 to "Jim":

<?php 
$hostname = "localhost"; 
$username = "root"; 
$password = ""; 
$databaseName = "my_database"; 

// 创建数据库连接 
$connect = mysqli_connect($hostname, $username, $password, $databaseName); 

// 检查连接是否成功 
if (!$connect) { 
  die("连接失败: " . mysqli_connect_error()); 
} 

// 更新数据 
$sql = "UPDATE users SET firstname='Jim' WHERE id=1"; 

if (mysqli_query($connect, $sql)) { 
  echo "数据成功更新到数据库表中"; 
} else { 
  echo "数据更新失败: " . mysqli_error($connect); 
} 

// 关闭数据库连接 
mysqli_close($connect); 
?>
  1. Delete data

Delete data from a database table using PHP, You need to use the mysqli_query() function. The following is a sample code to delete data with user ID 1:

<?php 
$hostname = "localhost"; 
$username = "root"; 
$password = ""; 
$databaseName = "my_database"; 

// 创建数据库连接 
$connect = mysqli_connect($hostname, $username, $password, $databaseName); 

// 检查连接是否成功 
if (!$connect) { 
  die("连接失败: " . mysqli_connect_error()); 
} 

// 删除数据 
$sql = "DELETE FROM users WHERE id=1"; 

if (mysqli_query($connect, $sql)) { 
  echo "数据成功从数据库表中删除"; 
} else { 
  echo "数据删除失败: " . mysqli_error($connect); 
} 

// 关闭数据库连接 
mysqli_close($connect); 
?>

3. Use PHP to implement data retrieval function

To use PHP to retrieve data from a database table, you need to use the mysqli_query() function . The following is a sample code that retrieves data for all users:

<?php 
$hostname = "localhost"; 
$username = "root"; 
$password = ""; 
$databaseName = "my_database"; 

// 创建数据库连接 
$connect = mysqli_connect($hostname, $username, $password, $databaseName); 

// 检查连接是否成功 
if (!$connect) { 
  die("连接失败: " . mysqli_connect_error()); 
} 

// 查询数据 
$sql = "SELECT * FROM users"; 
$result = mysqli_query($connect, $sql); 

if (mysqli_num_rows($result) > 0) { 
  while ($row = mysqli_fetch_assoc($result)) { 
    echo "username: " . $row["username"] . " - email: " . $row["email"] . "<br>"; 
  } 
} else { 
  echo "0 结果"; 
} 

// 关闭数据库连接 
mysqli_close($connect); 
?>

The above is an introduction to how to use PHP to implement data storage and retrieval functions. Mastering these techniques can help developers build powerful web applications more easily.

The above is the detailed content of How to use PHP to implement data storage and retrieval functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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