Interactive methods and techniques for PHP arrays and databases

WBOY
Release: 2023-07-15 12:54:01
Original
677 people have browsed it

Interaction methods and techniques between PHP arrays and databases

Introduction:
In PHP development, operating arrays is a basic skill, and interaction with databases is a common requirement. This article will introduce how to use PHP to manipulate arrays and interact with databases, and provide some practical code examples.

1. Basic operations of PHP arrays
In PHP, arrays are a very useful data type that can be used to store and operate a set of related data. The following are some common examples of array operations:

  1. Create an array:
    $arr ​​= array("apple", "banana", "orange");
  2. Access the array Element:
    echo $arr[0]; // Output: apple
  3. Add element to array:
    $arr[] = "grape"; // Add element to the end of array
    array_push($arr, "pineapple"); //Similarly, add elements to the end of the array
  4. Delete array elements:
    unset($arr[1]); //Delete the element at the specified index
    array_pop($arr); // Delete the element at the end of the array
  5. Traverse the array:
    foreach($arr as $key => $value) {

    echo "索引:".$key.",值:".$value;
    Copy after login

    }

2. Interaction with the database
Interaction with the database is a common task in Web development. The following will introduce how to use PHP to perform basic database operations, with code examples.

  1. Connect to the database:
    $servername = "localhost";
    $username = "root";
    $password = "password ";
    $dbname = "myDB";

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn-> connect_error) {

    die("数据库连接失败: " . $conn->connect_error);
    Copy after login

    }
    ?>

  2. Query data:
    $sql = "SELECT id, name , email FROM users";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "
    "; }
    Copy after login

    } else {

    echo "没有找到数据!";
    Copy after login

    }
    ?>

  3. Insert data:
    $sql = "INSERT INTO users (name , email) VALUES ('John Doe', 'john@example.com')";

    if ($conn->query($sql) === TRUE) {

    echo "新记录插入成功!";
    Copy after login

    } else {

    echo "插入数据时出错: " . $conn->error;
    Copy after login

    }
    ?>

  4. Update data:
    $sql = "UPDATE users SET email='john.doe@example.com' WHERE id=1";

    if ($conn->query($sql) === TRUE) {

    echo "数据更新成功!";
    Copy after login

    } else {

    echo "更新数据时出错: " . $conn->error;
    Copy after login

    }
    ?>

  5. Delete data:
    $sql = "DELETE FROM users WHERE id= 1";

    if ($conn->query($sql) === TRUE) {

    echo "数据删除成功!";
    Copy after login

    } else {

    echo "删除数据时出错: " . $conn->error;
    Copy after login

    }
    ?> ;

3. Conclusion
This article introduces the basic methods of operating arrays in PHP development, and provides some sample codes for interacting with the database. Mastering these methods and techniques will be of great help to PHP development and interaction with databases. I hope this article can inspire and help readers.

The above is the detailed content of Interactive methods and techniques for PHP arrays and databases. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!