首页 > 编程地带 > PHP-Mysql数据库编程

PHP-Mysql数据库编程

2007年10月28日 发表评论 阅读评论

今天就学了这些,明天再学查询。
一、连接和关闭数据库
    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]);

    示例:
    

<br />    <?php<br />      $connectmysql1=mysql_connect("localhost","username","password");<br />      echo $connectmysql."<br>";<br />      $connectmysql2=mysql_pconnect("lcoalhost","username","password");<br />      echo $connectmysql2."<br>";<br />    ?><br />

  
    2.关闭数据库连接    
    格式如下:
    

int mysql_close(int&#91;link_identifier&#93;)

    
    示例:
    [code=php]    
            $connection=mysql_close(“localhost”,”username”,”password”);
        echo $connection.”
“;
        $close=mysql_close($connection);
        echo $close;
    ?>
    [/code]    
二、创建、选择、删除数据库
    如下格式:
    

<br />    1.int mysql_create_db(string datebase_name,int&#91;link_identifier&#93;)<br />    2.int mysql_select_db(string datebase_name,int&#91;link_identifier&#93;)<br />    3.int mysql_drop_db(string datebase_name,int&#91;link_identifier&#93;)

    
    示例:    
    

<br />        <?php<br />        $connection=mysql_connect("localhost","username","password");<br />        if($connection)<br />        {<br />          //建立数据库<br />          $create_db=mysql_create_db("books",$connection);<br />          if(!$create_db)<br />              echo "create datebase failed!<br>";<br />          else{<br />              echo "create datebase sueccessfully!<br>";<br />          //选择数据库<br />              $select_db=mysql_select_db("books",$connection);<br />              if(!select_db)<br />                  echo "select datebase failed!<br>";<br />              else<br />                  echo "select datebase sueccessfully!<br>";<br />          //删除数据库<br />          $dorp_db=mysql_dorp_db("books",$connection);<br />          if(!$dorp)<br />              echo "drop datebase failed!<br>";<br />          else<br />              echo "drop datebase sueccessfully!<br>";<br />          }<br />       }<br />    ?>    <br />

    
三、增删改数据
    1.提交sql语句

int mysql_query(string query,int&#91;link_identifier&#93;);<br /> int mysql_db_query(strint datebase_name,string query,int&#91;link_identifier&#93;)

      
      示例:
      

     <br />      <?php<br />        $connection=mysql_connect("localhost","username","password");<br />        if($connection)<br />        {<br />          //建立数据库<br />          $create_db=mysql_create_db("books",$connection);<br />          if(!$create_db)<br />              echo "create datebase failed!<br>";<br />          else{<br />              echo "create datebase sueccessfully!<br>";<br />          //选择数据库<br />              $select_db=mysql_select_db("books",$connection);<br />          //创建表computers<br />          $query="create table computers(";<br />          $query.="id int(3) auto_increment not null primary key,";<br />          $query.="name char(10) not null,";<br />          $query.="price float default 0,";<br />          $query.="publish_date date)";<br />          echo $query."<br>";<br />          $result=mysql_query($query);<br />          if(!$result)<br />              echo "create table failed!<br>";<br />          else{<br />              echo "create table successfully!<br>";<br />              //插入一条记录<br />              $query="insert into computers(name,price,publish_date)";<br />              $query.="values(&#039;数据结构&#039;,20.00,&#039;2001-1-1&#039;)";<br />              echo $query."<br>";<br />              $result=mysql_query($query);<br />              if(!$query)<br />                 echo "insert date failed!<br>";<br />              else<br />                 echo "insert date successfully!<br>";<br />                 //修改记录<br />                 $query="update computers ";<br />                 $query.="set price=price+2 ";<br />                 $query.="where name=&#039;数据结构&#039;";<br />                 echo $query."<br>";<br />                 $result=mysql_query($query);<br />                 if(!$query)<br />                    echo "update data failed!<br>";<br />                 else <br />                    echo "update data successfully!<br>";<br />                 //删除记录<br />                 $query="delete from computers ";<br />                 $query.="where name=&#039;数据结构&#039;";<br />                 echo $query."<br>";<br />                 $result=mysql_query($query);<br />                 if(!$query)<br />                    echo "delete data failed!<br>";<br />                 else<br />                    echo "delete date successfully!<br>";<br />           }<br />       }<br />    }<br />   ?>   <br />

      

转载请注明:woyigui's blog [http://www.woyigui.cn/]
本文标题:PHP-Mysql数据库编程
本文地址:http://www.woyigui.cn/2007/10/28/php-mysql-database-programming/

分类: 编程地带 标签: ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
*