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 results";
$conn->close();
?></pre><p>2、Java代码示例:</p><pre class="brush:java;toolbar:false">
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>
还没有评论,来说两句吧...