您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

一些简单的PHP连接数据库例子详解_PHP教程

2025/11/16 7:22:12发布16次查看
本文章来给各位php入门者详细关于php连接数据库的实例代码,这里主要讲到了入门级的mysql连接代码到高级的封装数据库连接类,希望此文章对各位朋友所有帮助。
连接mysql数据库的两种方法:
(1)利用php的数据库函数连接
此方式是最常用的一种方式.
这里主要用到四个数据库函数:
mysql_connect () 建立与mysql服务器的连接。
mysql_select_db ():选择mysql服务器中的数据库供以后的数据查询操作query处理。
mysql_query ():送出query字符串以帮助mysql做相关的处理或执行。
mysql_fetch_row ():用来将查询结果result单行移到数组变量中。数组的索引是数字
索引,第一个索引值是0。
(2)通过odbc连接
php通过odbc连接mysql数据库主要用到四个函数:
odbc_connect ():用来同odbc数据源建立连接。
odbc_do ():用来在建立连接之后执行数据库查询。
odbc_result():用于取得当前记录行中某个字段的值。
odbc_fetch_row ():用来把查询结果保存到数组,每个数组元素对应一条记录。
我们先来看php的数据库函数连接 方法实例
连接到一个 mysql 数据库
在您能够访问并处理数据库中的数据之前,您必须创建到达数据库的连接。
在 php 中,这个任务通过 mysql_connect() 函数完成。
语法
mysql_connect(servername,username,password);参数 描述
servername 可选。规定要连接的服务器。默认是 localhost:3306。
username 可选。规定登录所使用的用户名。默认值是拥有服务器进程的用户的名称。
password 可选。规定登录所用的密码。默认是 。
 代码如下 复制代码
面向对象mysqli(详细教程)
 代码如下 复制代码
pdo连接mysql(详细教程)
 代码如下 复制代码
query('select * from user') as $row){
  print_r($row);
 }
 $db = null;  //关闭数据库
} catch (pdoexception $e) {
 echo $e->getmessage();
}
?>
然后我们还可以使用odbc连接数据库
 代码如下 复制代码
connect('localhost','root','','test');
$conn->execute(set names utf8);
$res = $conn->execute(select * from user);
if (!$res){
 echo $conn->errormsg();
}else{
 var_dump($res);
}
?>
mysql数据连接类
 代码如下 复制代码
dbtype) {
            case 1:
                $this->dblink = @mysql_pconnect($dbhost, $dbuser, $userpassword); // or die(can't connect to remote host!);
                @mysql_select_db($database, $this->dblink); // or die (can't connect to remote host!);
                break;
            case 2:
                break;
        }
        return true;
    }
    /* sql:select() 返回为false无结果 */
    function select($table, $columns, $condition = 1) {
        $sql = select $columns from $table where $condition ;
        $this->result = @mysql_query($sql, $this->dblink);
        unset($this->rows);
        if ($this->result) {
            $i = 0;
            if (!($this->rows = array($i => @mysql_fetch_array($this->result))))
                return false;
            if (($this->numrows = @mysql_num_rows($this->result)) == 0)
                return false;
            while ($temprows = @mysql_fetch_array($this->result)) {
                array_push($this->rows, $temprows);
            }
        } else {
            $this->halt($sql);
            return false;
        }
        return true;
    }
    /* sql:getrows() 返回查询的记录总数 */
    function getrows($table, $condition = 1) {
        $sql = select count(1) as count from $table where $condition;
        $this->result = @mysql_query($sql, $this->dblink);
        if ($this->result) {
            $temp = @mysql_fetch_array($this->result);
            $this->numrows = $temp[count];
        } else {
            $this->halt($sql);
            return false;
        }
        return $this->numrows;
    }
    /* sql:insert() */
    function insert($table, $columns, $values) {
        $sql = insert into $table ($columns) values ($values);
        $this->result = @mysql_query($sql, $this->dblink);
        if ($this->result)
            $this->insid = @mysql_insert_id($this->dblink);
        else {
            $this->halt($sql);
            return false;
        }
        return true;
    }
    /* sql:update() */
    function update($table, $setings, $condition) {
        $sql = update $table set $setings where $condition;
        $this->result = @mysql_query($sql, $this->dblink);
        if ($this->result)
            $this->numrows = @mysql_affected_rows($this->result);
        else {
            $this->halt($sql);
            return false;
        }
        return true;
    }
    /* sql:delete */
    function delete($table, $condition) {
        $sql = delete from $table where $condition;
        $this->result = @mysql_query($sql, $this->dblink);
        if ($this->result)
            $this->numrows = @mysql_affected_rows($this->result);
        else {
            $this->halt($sql);
            return false;
        }
        return true;
    }
    /* halt():error message */
    function halt($msg) {
        if ($this->msgflag == yes) {
            printf(database query error: %s
n, $msg);
            printf(mysql error: %s
n, mysql_error());
        }else
            echo ; //自定一个出错提示文件
        return false;
    }
}
switch ($db->dbtype) {
    case 1:
        @mysql_close();
        break;
    case 2:
        break;
}
$db = new database();
?>
友情提示
如果出现连接mysql数据库中文乱码我们可以在连接数据库查询之前加上mysql_query(set names utf8); 如果你是gbk就使用gbk编编码了
http://www.bkjia.com/phpjc/630682.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/630682.htmltecharticle本文章来给各位php入门者详细关于php连接数据库的实例代码,这里主要讲到了入门级的mysql连接代码到高级的封装数据库连接类,希望此文章...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product