PHP 数据库相关函数

0、PHP选择mysql的原因,强大、免费。oracle后期维护成本太大
1、连接到一个 MySQL 数据库
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
	die('Could not connect: ' . mysql_error());
}
//创建数据库
if (mysql_query("CREATE DATABASE my_db",$con))
{
	echo "Database created";
}
else
{
	echo "Error creating database: " . mysql_error();
}
//创建数据表
// Create table in my_db database
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons 
(
	FirstName varchar(15),
	LastName varchar(15),
	Age int
)";
mysql_query($sql,$con);
//插入数据
mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')");
//查询数据表
mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

while($row = mysql_fetch_array($result))
{
	echo $row['FirstName'] . " " . $row['LastName'];
	echo "
"; } mysql_close($con);

你可能感兴趣的:(PHP 数据库相关函数)