一、连接和关闭数据库
1.连接数据库
php通过mysql提供的API与mysql连接。mysql提供了两个用于连接数据库的函数,格式如下:
int mysql_connect(string[hostname[:port][:/path/to/socket]],string[username],string [password]);
int mysql_pconnect(string[hostname[:port][:/path/to/socket]],string[username],string[password]);
示例:
$connectmysql1=mysql_connect("localhost","username","password");
echo $connectmysql."
";
$connectmysql2=mysql_pconnect("lcoalhost","username","password");
echo $connectmysql2."
";
?>
2.关闭数据库连接
格式如下:
int mysql_close(int[link_identifier])
示例:
[code=php]
$connection=mysql_close("localhost","username","password");
echo $connection."
";
$close=mysql_close($connection);
echo $close;
?>
[/code]
二、创建、选择、删除数据库
如下格式:
1.int mysql_create_db(string datebase_name,int[link_identifier])
2.int mysql_select_db(string datebase_name,int[link_identifier])
3.int mysql_drop_db(string datebase_name,int[link_identifier])
示例:
$connection=mysql_connect("localhost","username","password");
if($connection)
{
//建立数据库
$create_db=mysql_create_db("books",$connection);
if(!$create_db)
echo "create datebase failed!
";
else{
echo "create datebase sueccessfully!
";
//选择数据库
$select_db=mysql_select_db("books",$connection);
if(!select_db)
echo "select datebase failed!
";
else
echo "select datebase sueccessfully!
";
//删除数据库
$dorp_db=mysql_dorp_db("books",$connection);
if(!$dorp)
echo "drop datebase failed!
";
else
echo "drop datebase sueccessfully!
";
}
}
?>
三、增删改数据
1.提交sql语句
int mysql_query(string query,int[link_identifier]);
int mysql_db_query(strint datebase_name,string query,int[link_identifier])
示例:
$connection=mysql_connect("localhost","username","password");
if($connection)
{
//建立数据库
$create_db=mysql_create_db("books",$connection);
if(!$create_db)
echo "create datebase failed!
";
else{
echo "create datebase sueccessfully!
";
//选择数据库
$select_db=mysql_select_db("books",$connection);
//创建表computers
$query="create table computers(";
$query.="id int(3) auto_increment not null primary key,";
$query.="name char(10) not null,";
$query.="price float default 0,";
$query.="publish_date date)";
echo $query."
";
$result=mysql_query($query);
if(!$result)
echo "create table failed!
";
else{
echo "create table successfully!
";
//插入一条记录
$query="insert into computers(name,price,publish_date)";
$query.="values('数据结构',20.00,'2001-1-1')";
echo $query."
";
$result=mysql_query($query);
if(!$query)
echo "insert date failed!
";
else
echo "insert date successfully!
";
//修改记录
$query="update computers ";
$query.="set price=price+2 ";
$query.="where name='数据结构'";
echo $query."
";
$result=mysql_query($query);
if(!$query)
echo "update data failed!
";
else
echo "update data successfully!
";
//删除记录
$query="delete from computers ";
$query.="where name='数据结构'";
echo $query."
";
$result=mysql_query($query);
if(!$query)
echo "delete data failed!
";
else
echo "delete date successfully!
";
}
}
}
?>
小编推荐:欲学习电脑技术、系统维护、网络管理、编程开发和安全攻防等高端IT技术,请 点击这里注册账号,公开课频道价值万元IT培训教程免费学,让您少走弯路、事半功倍,好工作升职加薪!
免责声明:本站系公益性非盈利IT技术普及网,本文由投稿者转载自互联网的公开文章,文末均已注明出处,其内容和图片版权归原网站或作者所有,文中所述不代表本站观点,若有无意侵权或转载不当之处请从网站右下角联系我们处理,谢谢合作!