PHP is a great web development option for beginners, leveraging basic concepts like variables, data types, conditional statements, and loops to build dynamic web applications. Contains a practical case showing how to use PHP to create a simple message board, involving steps such as establishing a connection, creating a table, adding messages, and displaying messages.
Transform Your Web Presence: PHP for Beginners
Introduction
PHP is a widely An open source programming language used to create dynamic and interactive web applications. It is a great option for beginners to learn web development. This article will take you through the basics of PHP and demonstrate its application through a practical case.
PHP Basics
Practical case: Create a simple message board
Step 1: Establish a connection
$servername = "localhost"; $username = "root"; $password = ""; $dbname = "myDB"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接 if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
Step 2: Create message table
// SQL 语句 $sql = "CREATE TABLE messages ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, message TEXT NOT NULL, created_at TIMESTAMP )"; // 执行查询 if ($conn->query($sql) === TRUE) { echo "Table messages created successfully"; } else { echo "Error creating table: " . $conn->error; }
Step 3: Add a message
// 准备语句 $stmt = $conn->prepare("INSERT INTO messages (name, message, created_at) VALUES (?, ?, NOW())"); // 绑定参数 $stmt->bind_param("ss", $name, $message); // 执行查询 if ($stmt->execute()) { echo "Message added successfully"; } else { echo "Error adding message: " . $stmt->error; }
Step 4: Display message
// SQL 语句 $sql = "SELECT * FROM messages"; // 执行查询 $result = $conn->query($sql); // 输出查询结果 while ($row = $result->fetch_assoc()) { echo "ID: " . $row["id"] . " - " . "Name: " . $row["name"] . " - " . "Message: " . $row["message"] . "
"; }
Conclusion
Through the above simple practical case, you can appreciate the powerful functions of PHP. By understanding basic concepts like variables, data types, conditional statements, and loops, you can start creating your own dynamic web applications.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3