<?php
@$con = mysql_connect(‘localhost‘,‘zsg‘,‘123456‘);
if(!$con){
die(‘Could not connect:‘ . mysql_error());
}
mysql_select_db("my_db",$con);
//建立一个数据库
/* if(mysql_query("create database my_db",$con)){
echo "database created";
}else{
echo "Error creating database :" . mysql_error();
} */
//建立一个数据表 CREATE TABLE
//主键字段永远要编入索引,这条规则没有例外,你必须对主键字段进行索引。这样数据库引擎才能快速定位给予改建数值的行 对于主键 还需添加NOT NULL 设置。
/* mysql_select_db("my_db",$con);
$sql = "CREATE TABLE Persons(
Id int(11) primary key auto_increment,
Firstname varchar(15),
Lastname varchar(15),
Age int(11)
)";
if(mysql_query($sql,$con)){
echo "TABLE Persons created successfully";
} */
//向数据库插入新纪录
/* $query = mysql_query("insert into Persons(Firstname,Lastname,Age)VALUES(‘张三‘,‘李四‘,‘5‘)");
if($query){
echo "成功插入数据";
} */
//向数据库查询数据,以表格的形式输出
//
//对于result的处理
//where 指定条件查询
//order by 排序
$result = mysql_query("select * from Persons order by Age ASC");
var_dump($result);
echo ‘<center><table border="" width="300px" height="200px" style="text-align:center; background-color:yellow;">‘;
echo "<tr><th>Firstname</th><th>Lastname</th><th>Age</th></tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row[‘Firstname‘]."</td>";
echo "<td>".$row[‘Lastname‘]."</td>";
echo "<td>".$row[‘Age‘]."</td>";
echo "</tr>";
}
echo ‘</table></center>‘;
//对数据库进行更新
/* $sql = mysql_query("update Persons set Age = ‘25‘ where Firstname = ‘张三‘ and Lastname = ‘李四‘");
if($sql){
echo "成功更新数据";
} */
mysql_close($con);
// Internet 信息服务器 (IIS) Internet information services