您当前的位置: 首页 > 网站编程 > PHP教程 > php之连接mysql

php之连接mysql

作者:不详 来源:网络 发布时间: 2014-07-15 22:30 点击:
1、两个连接函数 int mysql_connect(string[hostname][:port],string[username],string[password]) int mysql_pconnect(string[hostname][:port],string[username],string[password]) 这两个函数的区别在于,用mysql_connect()函数进行数据库系统链接时,操作数据库结

php之连接mysql

  1、两个连接函数

  int mysql_connect(string[hostname][:port],string[username],string[password])

  int mysql_pconnect(string[hostname][:port],string[username],string[password])

    这两个函数的区别在于,用mysql_connect()函数进行数据库系统链接时,操作数据库结束之后,会 自动关闭此连接,除非在此之        前已经用mysql_close()函数进行了强制关闭。而用mysql_pconnect() 函数进行连接时,都会自动检查是否有使用同样的主机名、        端口、用户名和密码进行的连接,如果有,则直接使用原有连接号,而且用mysql_pconnect()函数建立起来的连接是一种永久连           接,即使用mysql_close()函数也不会关闭此连接,只有退出整个Mysql数据库系统时才会关闭此连接。

     例如: 

     $connect=mysql_connect("localhost","root","root");

     echo $connect."<br>";

     mysql_close();

     $pconnect=mysql_pconnect("localhost","root","root");

     echo $pconnect;

     echo "<p>";

  2、关闭函数

  int mysql_close(int[linkid])

     该函数将关闭与Mysql数据库系统的连接,参数linkid为要关闭的连接标识,如果未指定此参数,函数将关闭最近一次打开的数据库       系统连接。如果连接关闭成功,函数将返回True,否则返回False

  3、更改用户函数

  int mysql_change_user(string user,string password,string [database],int[linkid])

     该函数用于改变当前与数据库系统相连接的用户,参数user为用户名,password为密码,如果二者

     不相符合,则不改变数据库系统当前用户。如果指定了数据库database,则此数据库将为新用户的

     默认数据库,参数linkid为数据库系统的连接标识,默认为最近一次创建的系统连接。

  4、创建数据库函数

  int mysql_create_db(string databasename,int[linkid])

  5、删除数据库函数

  int mysql_drop_db(string databasename,int[linkid])

  6、选择数据库函数

  int mysql_select_db(string databasename,int[linkid])

    例如:

    $connect=mysql_connect("localhost","root","root");

    echo mysql_create_db("php1")."<br>";

    echo mysql_create_db("php2")."<br>";

    echo mysql_drop_db("php2")."<br>";

    mysql_close();

    例如:

    $connect=mysql_connect("localhost","root","root");

    echo mysql_select_db("test")."<br>";

    mysql_close();

  7、查询函数

  int mysql_query(string query,int[linkid])

  int mysql_db_query(string database,string query,int[linkid])

    这两个函数的区别在于mysql_query()函数需要使用mysql_select_db()函数预先选择数据库,而mysql_db_query()函数的数据库选         择在参数之中。

    例如:

    $connect=mysql_connect("localhost");

    mysql_select_db("test");

    $result1=mysql_query("select * from zgsj_js");

    $result2=mysql_db_query("test","select * from zgsj_js");

    mysql_close();

  8、记录函数

  int mysql_affected_rows(int[link_id])

     此函数用于返回MYSQL服务器最近一次操作所影响的记录所在行号。如果没有指定参数link_id,则使用最近一次打开的连接。

  int mysql_data_seek(int result_identifier,int row_number)

     此函数用于在数据库记录集中移动记录指针,参数row_number指定了要移动到的记录行。如果移动成功则返回True,

     否则返回False。

  array mysql_fetch_array(int result,int[result_type])

  array mysql_fetch_row(int result)

     mysql_fetch_array()函数的作用是获得数据库当前行的记录,返回以字段名及数值索引为下标数组

     mysql_fetch_row()函数的作用与上相同,返回只能以数值索引为下标的数组

  object mysql_fetch_object(int result,int[result_type])

     此函数的作用是将数据库记录集当前指针所指记录以对象的形式返回,同时指针后移一位。

     例如:记录显示1

     $connect=mysql_connect("localhost","root","root");

     mysql_select_db("test");

     $result=mysql_query("select * from zgsj_js");

     echo"<table border=1>";

     echo"<tr><td>name</td><td>id</td><td>number</td><td>sexuality</td></tr>";

     while($arr=mysql_fetch_array($result))

       {

         echo"<tr><td>".$arr["name"]."</td>";

         echo"<td>".$arr["id"]."</td>";

         echo"<td>".$arr["number"]."</td>";

         echo"<td>".$arr["sexuality"]."</td></tr>";

       }

      echo"</table>";

      例如:记录显示2

      $connect=mysql_connect("localhost","root","root");

      $result=mysql_db_query("test","select * from zgsj_js");

      while($row=mysql_fetch_object($result))

        {

          echo"name:".$row->name.",";

          echo"id:".$row->id.",";

          echo"number:".$row->number.",";

          echo"sexuality:".$row->sexuality."<br>";

        }

       mysql_free_result($result);
分享到:
本文"php之连接mysql"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4