黑基网 首页 学院 电脑技术 查看内容

PHP实现Mysql的操作

2009-10-15 09:13| 投稿: computer

摘要: 一、连接和关闭数据库      1.连接数据库      php通过mysql提供的API与mysql连接。mysq...
一、连接和关闭数据库      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]);      示例:           <?php        $connectmysql1=mysql_connect("localhost","username","password");        echo $connectmysql."<br>";        $connectmysql2=mysql_pconnect("lcoalhost","username","password");        echo $connectmysql2."<br>";      ?>              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])           示例:                   <?php          $connection=mysql_connect("localhost","username","password");          if($connection)          {            //建立数据库            $create_db=mysql_create_db("books",$connection);            if(!$create_db)                echo "create datebase failed!<br>";            else{                echo "create datebase sueccessfully!<br>";            //选择数据库                $select_db=mysql_select_db("books",$connection);                if(!select_db)                    echo "select datebase failed!<br>";                else                    echo "select datebase sueccessfully!<br>";            //删除数据库            $dorp_db=mysql_dorp_db("books",$connection);            if(!$dorp)                echo "drop datebase failed!<br>";            else                echo "drop datebase sueccessfully!<br>";            }        }      ?>               三、增删改数据      1.提交sql语句 int mysql_query(string query,int[link_identifier]); int mysql_db_query(strint datebase_name,string query,int[link_identifier])               示例:                    <?php          $connection=mysql_connect("localhost","username","password");          if($connection)          {            //建立数据库            $create_db=mysql_create_db("books",$connection);            if(!$create_db)                echo "create datebase failed!<br>";            else{                echo "create datebase sueccessfully!<br>";            //选择数据库                $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."<br>";            $result=mysql_query($query);            if(!$result)                echo "create table failed!<br>";            else{                echo "create table successfully!<br>";                //插入一条记录                $query="insert into computers(name,price,publish_date)";                $query.="values('数据结构',20.00,'2001-1-1')";                echo $query."<br>";                $result=mysql_query($query);                if(!$query)                  echo "insert date failed!<br>";                else                  echo "insert date successfully!<br>";                  //修改记录                  $query="update computers ";                  $query.="set price=price+2 ";                  $query.="where name='数据结构'";                  echo $query."<br>";                  $result=mysql_query($query);                  if(!$query)                      echo "update data failed!<br>";                  else                      echo "update data successfully!<br>";                  //删除记录                  $query="delete from computers ";                  $query.="where name='数据结构'";                  echo $query."<br>";                  $result=mysql_query($query);                  if(!$query)                      echo "delete data failed!<br>";                  else                      echo "delete date successfully!<br>";            }        }      }    ?>  
小编推荐:欲学习电脑技术、系统维护、网络管理、编程开发和安全攻防等高端IT技术,请 点击这里 注册黑基账号,公开课频道价值万元IT培训教程免费学,让您少走弯路、事半功倍,好工作升职加薪!



免责声明:本文搜集整理自互联网,版权归原作者所有,文中所述不代表本站观点,若有侵权或转载等不当之处请联系我们处理,请我们一起为维护良好的互联网秩序而努力!联系方式见网站首页右下角。


鲜花

握手

雷人

路过

鸡蛋

相关阅读

最新评论


新出炉

返回顶部