Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 操作系统 > Linux > PHP4.2.2 与 MySql3.2 的结合应用:
【标  题】:PHP4.2.2 与 MySql3.2 的结合应用:
【关键字】:PHP4.2.2,MySql3.2
【来  源】:http://blog.chinaunix.net/article.php?articleId=35845&blogId=7115

PHP4.2.2 与 MySql3.2 的结合应用:

Your Ad Here PHP4.2.2 与 MySql3.2 的结合应用:


    1.连接MySql服务器时,可以使用“mysql_connect”函数,语法格式为:
                             int mysql_connect(hostname:port,username,password)
                       返回值:整型,表示本次连接的ID好。
                       参数: hostname表示要登录的SQL服务器的名称。username表示登录服务器使用的用户名;password表示用户密码。
      示例:

         <?php
               $username="root";
               $passwd="";  /*初始安装mysql时root密码为空。*/
              //连接数据库:
               $link_mess=mysql_connect("localhost",$username,$passwd);
              //测试连接:
              if(!$link_mess){
                       echo "fail to connect with the sql server";
                       exit();
                             }
               else
                     {
                         echo "success to connect with mysql server!";
                     }
         ?>   
    2.要关闭数据库连接时,可以用“mysql_close()”函数,关闭成功返回“True”,否则返回“False”.语法格式如下:
                              mysql_close(变量)
     示例:<html>
           <head>
           <title>close the db</title>
           </head>
           <body>
           <?php
                 $myconn=mysql_connect("localhost","root","");
                 if(mysql_close($myconn)){
                                      echo "success to close the db connection";
                                        }
                 else
                     {
                         echo "fail to close the db connection";
                     }
           ?>
           </body>
           </html>
        NOTE:如果使用mysql_close()时没有指定变量,则mysql_close()会关闭最近所连接的数据库。因此在使用时需要先确定要关闭的数据库连接,在选择是否加上参数。
   3.数据库的选择,应用函数:mysql_select_db(),主要选取所要使用的数据库,如果成功就返回“True”,若找不到数据库或选取失败则返回“False”.语法格式如下:
                                 mysql_select_db(数据库名称)
                   使用此函数的前提是:已经使用,mysql_connect()函数连接到mysql数据库服务器了。
       示例:〈html>
              <head>
              <title>数据库的选择〈/title〉
             〈/head〉
        <body>
              <?php
                   $myconn=mysql_connect("localhost","root","");
                   if(mysql_select_db("lbuser"){
                             echo "lbuser has been selected";
                                               }
                   else
                        {
                             echo "fail to select the database lbuser";
                        }
              ?>
              </body>
              </html>
   4.发送SQL命令: 在读取数据库中的数据表之前,先要用mysql_query()函数来将sql命令传给,mysql来处理。该函数语法如下:
               mysql_query(sql命令或变量,链接指针)
       只要是sql命令,如:select,insert,delete,update,create table,drop table等,都可作为mysql_query()的参数。但是在使用该函数前,必须先要
       用mysql_select_db()函数来选择所要操作的数据库。
  5.读取数据表可使用mysql_fetch_field()函数,该函数语法如下:
         mysql_fetch_field(数据查询结果)
  下面是执行sql命令的例子:
       <html>
              <head>
              <title>数据库的选择</title>
              </head>
              <body>
              <?php
                    mysql_connect("localhost","root","");
                    mysql_select("lbuser");
                    $sql_string="select * from user"; //选取数据表赋值给$sql_string
                    $exec=mysql_query($sql_string);  //调用变量$sql_string执行sql命令
                    if($field=mysql_fetch_field($exec)){
                 echo "标题名称:field->name<br>";
                                  echo "所属数据表:$field->table<br>";
                                  echo "数据类型:$field->type<br>";
                                  echo "字段最大长度:$field->max_length<br>";
                                                      }
                    else
                        {
                             echo "there no relative infomation";
                        }
               ?>
               </body>
               </html>
    6.取得数据:该示例是在数据表中读取三条记录,并把读取结果的两个字段在浏览器中显示出来。其源码如下:
       <html>
              <head>
              <title>从数据库中打开记录</title>
              </head>
              <body>
              <?php
          $dbname="lbuser";
                    $dbtable="user";
                    $myconn=mysql_connect("localhost","root","");
                    mysql_select("lbuser");
                    $sql_string="select * from user order by ID"; //选取数据表赋值给$sql_string
                    $exec=mysql_query($sql_string,$myconn);  //调用变量$sql_string执行sql命令
                    mysql_close($myconn);                    //关闭数据库
                    for ($i=1;$i<4;i++)
                       {
                         $record=mysql_fetch_row($exec);  //取结果,以数组返回,放在$record中
                         echo "user:";
                         echo $record[1];
                         echo "<br>";
                         echo "";
                         echo $record[2];
                         echo "<br>";
                         echo "";
                        }
                ?>
               </body>
               </html>            

Display and Fonts:【上一篇】
Getting Started with SELinux HOWTO:the new SELinux:【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • Coreldraw 设计应用之邮票制作
  • (转贴)Visual C#编写3D游戏框架示例
  • 利用VB实现“绿色”软件<转>
  • CULT3D系列教程第三辑:粒子系统的相关设置
  • Windows 98 禁止非法用户登录的综合设置
  • 模版方法模式 - Template Method Pattern - 穷人和富人的不同婚恋历程
  • Windows Installer --王岩岚
  • 光盘上的Linux Knoppix 5.0.1发布
  • rdesktop用法
  • [分享]Hibernate中文参考手册
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.