1、PHP代码示例:
<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败:" . $conn->connect_error);
// 查询数据
$sql = "SELECT id, name FROM myTable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 结果";
$conn->close();
?>
2、Java代码示例:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个数字:");
int num = scanner.nextInt();
System.out.println("你输入的数字是:" + num);
}
}</pre><p>3、C++代码示例:</p><pre class="brush:cpp;toolbar:false">
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}</pre><p>代码分别展示了如何使用PHP、Java和C++连接到数据库、查询数据并输出结果,这些基本代码是网站建设的基础,通过学习和实践,你可以逐步掌握更多的编程技巧和方法。</p>
还没有评论,来说两句吧...