1-2-5_PHP-2

PHP To SQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
# 连接数据库
$con = mysql_connect("localhost","root","root");

if(!$con){
dir("Database Connect Error".mysql_error());
}else{
# 设定编码
mysql_query( "set names utf8" );

# 连接数据库
mysql_select_db("websafe",$con);

# 操作数据库
$result = mysql_query("select * from teacher");
echo "Show teachers in table<br />";
echo "<table border='2' width='300'>"
<tr><th>id</th><th>name</th><th>sex</th><th>addr</th> </tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row["id"]."</td>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["sex"]."</td>";
echo "<td>".$row["addr"]."</td>";
echo "</tr>";
}
echo "</table>";
}

# 关闭数据库
mysql_close($con);
?>

三种sql方法

  • mysql扩展
  • MYSQLi扩展
  • PDO扩展 (PHP Data Object)