(1)连接数据库代码
$LinkID=@mysql_connect("localhost", "root" , "") or die("不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!"); 
 
    其中,函数mysql_connect()用于与数据库服务器建立连接。三个参数分别为:数据库服务器的主机名(也可以是IP)、数据库用户名和用户密码。函数返回值用于表示这个数据库连接。    函数前面的“@”符号,用于限制这个命令的出错信息的显示。如果函数调用出错,将执行or后面的语句。die( )函数表示向用户输出引号中的内容后,程序终止执行。这样做是为了防止数据库连接出错时,用户看到一堆莫名其妙的专业名词,而是提示定制的出错信息。不过在调试的时候,我们还是可以不要屏蔽出错信息,免得出错后,难以找到到底哪里有问题。 @mysql_select_db("ResumeDB",$LinkID) or die("选择数据库出错,可能是您指定的数据库不存在!");     选择数据库时,要提供的参数是数据库的名称、和服务器连接号。   当我们只使用一台数据库服务器时,$LinkID可以省略,系统自动查找最近的一个数据库连接然后使用它。但是,当你要实现大型站点的时候,必不可少的要遇到多主机、多数据库系统的情况。这时,数据库连接参数就不能省略了。

解决方案 »

  1.   

    (2)查询数据库代码
    $Name= "OpenBall"; //实际操作中,$Name、$Intro为从浏览器表单传来的值   $Intro = "OpenBall的个人简介……";   $query = "insert into Resume(Name,Intro) values('$Name', '$Intro')"; //生成SQL语句   $result = @mysql_query("$query",$LinkID); //执行   if(! $result)    echo "数据插入失败!";   $query= "select ID,Name,Intro from Resume"; //生成SQL语句   $result = mysql_query($query,$LinkID); //执行,结果集保存到变量$result中   $num= mysql_num_rows($result); //取得查询返回的记录行数   if($num == 0)   {    echo "没有找到任何记录";    exit();   }   while($row=mysql_fetch_array($result)) //取结果集的下一行数据到数组$row中   {    echo $row["ID"]." ".$row["Name"]." ".$row["Intro"]."〈br〉";   //以字段名为索引访问数组变量的值   } 
        上面的操作,共执行了两次数据库操作。第一次为插入操作,第二次为查询操作。程序首先插入当前用户的一天记录,然后,显示所有数据库中的个人情况。
        @mysql_free_result($result); 
        @mysql_close($LinkID); 
        释放结果集,释放结果集和数据库连接资源
        建立持续连接的方法,就是在数据库连接的时候,调用函数mysql_pconnect()代替mysql_connect() 。建立的持续连接在本程序结束时,不需要调用mysql_close()来关闭。下次程序在此执行mysql_pconnect()时,系统自动直接返回已经建立的持续连接的ID号,而不再去真的连接数据库。
      

  2.   


    (3)关于SMARTY模板简单实用代码 先来看一个简单的例子。  ===================================================== 
     index.tpl 
     ===================================================== 
     
     {* 显示是smarty变量识符里的用*包含的文字为注释内容 *} 
     {include file="header.tpl"}{*页面头*} 
    大家好,我叫{$name}, 欢迎大家阅读我的smarty学习材料。 
     {include file="foot.tpl"}{*页面尾*} 上边的这个例子是一个tpl模板,其中: 
    1. {**}是模板页的注释,它在smarty对模板进行解析时不进行任何输出,仅供模板设计师对模板进行注释。 
    2. {include file="xxx.tpl"}使用此句将一个模板文件包含到当前页面中,例子中将在网站中公用事的head.tpl与foot.tpl进行了包含,你可以 
    这样想,使用这一句将xxx.tpl中的内容全部复制在当前语句处。当然,你不使用这一句也可以,将XXX.tpl中的内容复制到当前语句处 
    也是完全可以了。 3.{$name}: 模板变量,smarty中的核心组成,采用smarty定义的左边界符{与右边界符}包含着、以PHP变量形式给出,在smarty程序中将使用 
    $smarty->assign("name", "李晓军");将模板中的$name替换成“李晓军”三个字。 
     
    整个实例源程序如下: 
    ============================= 
    header.tpl 
    =============================  
     
     =============================== 
    foot.tpl 
    =============================== 
     
    --------------------------------------------------------------------------------
    CopyRight(C) by 大师兄 2004年8月 Email: [email protected]  
    -------------------------------------------------------------------------------- 
     ===================================================== 
     index.tpl 
     ===================================================== 
     
     {* 显示是smarty变量识符里的用*包含的文字为注释内容 *} 
     {include file="header.tpl"}{*页面头*} 
    大家好,我叫{$name}, 欢迎大家阅读我的smarty学习材料。 
     {include file="foot.tpl"}{*页面尾*} ================================================ 
    index.php 
    ================================================ 
    /********************************************* 
     * 
     * 文件名: index.php 
     * 作用: 显示实例程序 
     * 
     * 作者: 大师兄 
     * Email:[email protected] 
     * 
     *********************************************/ 
     include_once("./comm/Smarty.class.php"); //包含smarty类文件  $smarty = new Smarty();//建立smarty实例对象$smarty 
     $smarty->templates("./templates"); //设置模板目录 
     $smarty->templates_c("./templates_c"); //设置编译目录 
     
     //---------------------------------------------------- 
     //左右边界符,默认为{},但实际应用当中容易与JavaScript 
     //相冲突,所以建议设成<{}>或其它。 
     //---------------------------------------------------- 
     $smarty->left_delimiter = "{"; 
     $smarty->right_delimiter = "}";  $smarty->assign("name", "李晓军"); //进行模板变量替换 
     
     //编译并显示位于./templates下的index.tpl模板 
     $smarty->display("index.tpl"); 
    ?> 
     
    最终执行这个程序时将显示为: 
    ================================ 
    执行index.php 
    ================================ 
     
     
     
     
     
    大家好,我叫李晓军, 欢迎大家阅读我的smarty学习材料。 
    
    --------------------------------------------------------------------------------
     
    
    CopyRight(C) by 大师兄 2004年8月 Email: [email protected]  
    -------------------------------------------------------------------------------- 
      

  3.   

    少了个分页,给你个分页函数<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    </head><body>
    <h2 align="center">欣欣网站留言簿</h2>
    <hr>
    <?php
    // 建立与数据库服务器的连接
    global $hostnameglobal $hostname,$username,$password,$dbname
    $hostname="localhost";$cn=@mysql_connect("$hostname") or die("不能连接数据库服务器");
    //每页显示2条留言
    $pagesize=2;
    //定义一个SQL语句,从notes表中选出所有记录
    $strsql="select * from notes";
    //发送strsql语句
    $result=mysql_db_query("enote",$strsql.$cn);
    //获得总页数
    if (($total%$pagesize)==0){$totalpage=(int)($total/$pagesize);}
    else{$totalpage=(int)($total/$pagesize)+1;}
    if(!$page){
    //刚开始时,脚本没有接收到任何post内容,即$page为空,因而当前页数是1 
    $pageno=1;}
    else{
    //从表单中取得当前页号
      switch($page)
       {
         case"首页";
      $pageno=1;
        break;
     case"上一页";
      $pageno--
        break;
     case"下一页";
      $pageno++;
        break;
     case"尾页";
      $pageno=$totalpage;
        break
       }
    }
    //定义一个SQL语句,该语句按编号的递减次序从notes表中
    //选出从($pageno-1)*$pagesize开始的$pagesize条记录
    $startpos=($pageno-1)*$pagesize;
    $strsql="select * from notes order by nid desc "."limit $startpos,$pagesize";
    //发送strsql
    $result=mysql_db_query("enote",$strsql,$cn);
    //循环输出每条记录
    while($arr=mysql_fetch_query($result)
       {
         echo "<table>";
     echo "<tr><td><b>昵&nbsp;&nbsp;称:</b>".$arr[name];
     echo "&nbsp;&nbsp;<b>E-mail:</b>".$arr[email]."</td></tr>";
     echo "<tr><td><b>主&nbsp;&nbsp;题:</b>".$arr[subject]."</td></tr>";
     echo "<tr><td><b>时&nbsp;&nbsp;间:</b>".$arr[notedate]."</td></tr>";
     echo "<tr><td bgcolor='#ooffff'>".n12br($arr[comment])."</td></tr>";
     echo "</table><hr>";
       }
    //关闭连接
    mysql_close($cn);
    ?>
    <form action<?php echo $php_self ?> "method="post">
    <!--隐含域记录当前页号-->
    <input type="hidden" name="pageno" value=<?php echo($pageno) ?>>
    <input type="submit" name="page" value="首页">&nbsp;
    <?php if($pageno>1): ?>
    <input type="submit" name="page" value="上一页">&nbsp;
    <?php endif; ?>
    <?php if($pageno<>$totalpage): ?>
    <input type="submit" name="page" value="下一页">&nbsp;
    <?php endif;?>
    <input type="submit" name="page" value="尾页">&nbsp;
    <b>共<?php echo($total) ?>条留言&nbsp;页数:<?php echo($pageno) ?>/<?php echo(&totalpage) ?></>
    </form>
    </body>
    </html>
      

  4.   

    问题1,2当然可以用类来实现,其实3没必要用类做个就可以一个函数
    class databaseSQL{
        var $host_ip;
        var $name;
        var $pwd;
        var $db_name;
        var $old_db_name = array();
        var $conn;
        var $old_conn = array();
        var $result = NULL;
        var $limit = "ALL";
        var $fields = "*";
        var $tables = "userinfo";
        var $where = "TRUE";
        var $orders = NULL;
        var $groups = NULL;
        var $count = FALSE;         //if is going to get the count of records.    function Clear(){
            $this->limit = "ALL";
            $this->fields = "*";
            $this->tables = "userinfo";
            $this->where = "TRUE";
            $this->orders = NULL;
            $this->groups = NULL;
            $this->count = FALSE;
            return;
        }
        /**
        * convert result to a array.
        * @param pgresult $result the query result.
        * @return array
        */
        function ResultToArray($result){
        //把取得数据组织成你理想的数组形式
        }
        function DisplayResult($result = NULL, $arr2 = NULL, $flag = NULL){
        //这里可以做分页显示.
        }
        /**
        * Constructor
        * @param string $host_ip the HOST IP Address.
        * @param string $user_name the user name to visit the database.
        * @param string $user_pwd the user password to visit the database.
        */
        function databaseSQL($host_ip, $db_name, $user_name = NULL, $user_pwd = NULL){
            $this->host_ip = $host_ip;
            $this->db_name = $db_name;
            $this->name = $user_name;
            $this->pwd = $user_pwd;
            $this->Connect();
            $this->old_conn[$this->db_name] = $this->conn;
        }
        /**
        * Connect to the database.
        *XXX_connect()根据不同的数据库选择不同的函数.
        */
        function Connect(){
            $this->conn = XXX_connect("host=".$this->host_ip.
                                     " dbname=".$this->db_name.
                                     " user=".$this->name.
                                     " password=".$this->pwd
                                    );
        }
        /**
        * Get the query string from the given condition.
        * @return string the query string.
        */
        function GetSql(){
        //......
        }
        /**
        * Do a query
        * @param string $sql the query string.
        * @return pgresult
        */
        function Query($sql=NULL){
            if( NULL == $sql ){
                $sql = $this->GetSql();
            }
            
            $this->result = @XXX_query($this->conn,$sql);
            
            if( !$this->result ){
                echo($sql)."<BR><BR>";
                echo(XXX_last_error($this->conn)."<BR>");
                die;
            }
            return $this->result;
        //........
        }
    }
    并不是很复杂,可以参照php 手册慢慢搞.