数据访问,使用mysql类访问数据

数据访问分为三种

1.使用函数 在新版本里面废弃了

2.面向对象的方式 Mysqli类

3.PDO的方式

例子

<table width="100%" border="1">
    <tr>
        <td>代号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>民族</td>
        <td>生日</td>
    </tr>

使用Mysqli类来访问数据库:

1.在Mysqli的对象,相当于在PHP和mysql数据库中间建立了通道

$db = new MySQLi("localhost","root","123","mydb");

2.判断连接是否出错

if(mysqli_connect_error()){

echo "连接失败!";

exit; //如果连接出错,直接结束程序

}

3.写SQL语句

$sql = "select * from info";

4.执行准备好的SQL语句

$result = $db->query($sql);

如果执行的是查询语句,返回结果集对象,如果执行的是其它语句,返回true或false

5.从结果集对象里面读数据

$arr1 = $result->fetch_row();   每次读一条,返回数组

可以使用while循环读取所有数据

while($arr = $result->fetch_row()){

  var_dump($arr);

}

$arr = $result->fetch_all(); //读取所有,返回二维数组

var_dump($arr);

$arr = $result->fetch_assoc(); //读取一条,返回关联数组

var_dump($arr);

$arr = $result->fetch_object(); //读取一条,返回对象

var_dump($arr);

$arr = $result->fetch_all();

foreach($arr as $v){

//处理性别
    $sex = $v[2]?"男":"女";
    //民族处理
    $sql = "select name from nation where code=‘{$v[3]}‘";
    $re = $db->query($sql);
    $a = $re->fetch_row();

echo "<tr>
        <td>{$v[0]}</td>
        <td>{$v[1]}</td>
        <td>{$sex}</td>
        <td>{$a[0]}</td>
        <td>{$v[4]}</td>
    </tr>";
}

然后是连接新建的php文件,如下

$db = new MySQLi("localhost","root","123","mydb");
if(mysqli_connect_error()){
    die("连接失败!");
}
$sql = "insert into xuanxiang values(0,‘111111‘,‘a‘,1)";
if($db->query($sql)){
    echo "添加成功!";
    echo $db->insert_id; //取添加的主键值
}else{
    echo "添加失败!";
}

时间: 2025-01-05 01:33:48

数据访问,使用mysql类访问数据的相关文章

使用MySQL类进行数据访问

数据访问分为三种 1.使用函数 在新版本里面废弃了 2.面向对象的方式 Mysqli类 3.PDO的方式 例子 <table width="100%" border="1"> <tr> <td>代号</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> </tr>

mysql 删除重复数据

如题:mysql 数据库删除重复数据 因为是mysql 所以其他数据哭的命令在mysql 中是不能使用的.不要想当然的使用sql 脚本 delete from table1 where field1 in (select field1 from table1 group by field1 having count(field1) > 1) and rowid not in (select min(rowid) from table1 group by field1 having count(f

重要!!!实体类、数据访问类

创建两个类: users类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 实体类_数据访问类.App_Code { public class Users { private int _Ids; /// <summary> /// ids /// </summary> public int Ids { get { return _Ids;

实体类、数据访问类中的属性拓展

类中: using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; namespace 实体类_数据访问类.App_Code { public class Users { SqlConnection conn = null; SqlCommand cmd = null; public Users() { conn = new S

C#-ade.net-实体类、数据访问类

实体类.数据访问类 是由封装演变而来,使对数据的访问更便捷,使用时只需要调用即可,无需再次编写代码 实体类是按照数据库表的结构封装起来的一个类 首先,新建文件夹 App_Code ,用于存放数据库类等类文件,新建类,例如: Users(与数据库访问的表同名)和 UsersData 在类UsersData里写数据库访问方法 using System; using System.Collections.Generic; using System.Linq; using System.Text; us

实体类、数据访问类、属性扩展

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 实体类_数据访问类.App_Code { public class Users { private string _username; //封装 /// <summary> /// 用户名 /// </summary> public

【2017-04-20】Ado.Net与面向对象结合架构中的数据访问层(实体类,数据访问类)

开发项目三层架构:界面层.业务逻辑层.数据访问层 今天学习一下数据访问层,分为实体类和数据访问类 所有的类放在App_Code这个文件夹下边.养成一个好的习惯. 一.实体类 数据库中的表映射为一个类,类名与表名一致.表中的每一列,都为该类下的成员变量和属性也就是最简单的封装 把数据库中的表名变为类的类名. 把数据库中的每一个列,变为实体类中的成员变量和属性 列名与属性名一致.成员变量名:在列名前边加上下划线.因为在外部访问只能访问到属性,为了看起来一致. using System; using

ADO.NET(完整修改和查询、实体类,数据访问类)

一.完整修改和查询 在编写c#语句时需考虑到用户体验,例如在编写修改语句时,需要考虑到输入的内容在数据库中是否能够找到. 中间变量运用. 1.先查 2.执行操作 完整修改语句: bool has = false; Console.Write("请输入要修改的用户名:"); string Uname = Console.ReadLine(); //到数据库中查询输入的用户名是否存在 SqlConnection conn = new SqlConnection("server=.

ado.net 实体类_数据访问类

实体类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 实体类_数据访问类.App_Code { public class Users { private int _code; /// <summary> /// code /// </summary> public int Code { get { return _code; } set