Blogger Information
Blog 32
fans 0
comment 0
visits 27675
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP MySqli连接操作数据库
Yang_Sir
Original
780 people have browsed it

mysqli:只用于连接MySQL数据
mysqli对数据库的操作方法与PDO类似,可以通过query方法直接执行,也可以通过prepare()方法预处理

1.连接数据库

  • 创建一个mysqli连接对象,传入连接参数,连接数据库
  1. //配置数据库参数
  2. $config = [
  3. 'host'=>'127.0.0.1',
  4. 'dbname'=>'www.merchant.office',
  5. 'username'=>'merchant',
  6. 'password'=>'merchant',
  7. 'charset'=>'utf8',
  8. 'port'=>'3306',
  9. ];
  10. extract($config);//将关联数组扩展为变量,键为变量名,值为数组元素的值
  11. $mysqli = new mysqli($host, $username, $password, $dbname);//连接数据库
  12. if($mysqli->connect_errno)die('数据库连接失败:'.$mysqli->connect_errno);//判断是否连接失败,输出失败原因
  13. $mysqli->set_charset($charset);

2.创建一张商户信息表

  • 编写建表sql语句,调用mysqli中的query()方法执行
  1. $sql = "CREATE TABLE IF not EXISTS `goods`(
  2. `id` int(10) NOT NULL auto_increment,
  3. `name` VARCHAR(60) not null,
  4. `model` VARCHAR(30) ,
  5. `price` DECIMAL(8,2),
  6. `number` VARCHAR(8),
  7. `status` int(1) null DEFAULT 0 ,
  8. PRIMARY KEY(`id`)
  9. )ENGINE=INNODB DEFAULT CHARSET=utf8;";
  10. var_dump($mysqli->query($sql));//执行并打印执行结果

3.新增数据

  • foreach循环把数组中的数据处理后insert到数据表中
  • 通过参数绑定方式新增数据
  1. $goods=[['华为手机','P40','5999','300'],['小米手机','米10','2999','320'],['苹果手机','XR','8999','610'],
  2. ['华为手机','P30','3599','500'],['小米手机','米9','2099','300'],['苹果手机','8s','5999','110'],
  3. ];
  4. $i=0;
  5. $str = '';
  6. //循环插入数据
  7. foreach($goods as $value){
  8. array_walk($value,function(&$item){
  9. return $item = "'".$item."'";
  10. });
  11. $data = implode(",",$value);
  12. $sql = "insert into `goods`(`name`,`model`,`price`,`number`) value({$data})";
  13. //echo $sql,'<br>';
  14. if($mysqli->query($sql)) echo '插入第:'.++$i.'条<br>';//打印插入的条数
  15. }
  16. //通过参数绑定方式新增数据
  17. $sql = "insert into `goods`(`name`,`model`,`price`,`number`) value(?,?,?,?)";
  18. $query = $mysqli->prepare($sql);
  19. $name='oppo';$model='V8';$price='3599';$number='685';
  20. $query->bind_param('ssss',$name,$model,$price,$number);
  21. $query->execute();
  22. echo $mysqli->insert_id;

4.查询、修改、删除

  • mysqli执行query()方法后返回一个结果集对象,
  • 查询操作,通过fetch_assoc()fetch_all()fetch_row()等方法获取数据
  • 更新、删除操作,通过affected_rows属性获取受影响的记录数
  1. //查询数据
  2. $sql = "select * from goods where id>2";
  3. $res_fetch = $mysqli->query($sql);
  4. var_dump($res);
  5. print_r($res_fetch->fetch_assoc());//获取一条数据,返回关联数组
  6. print_r($res_fetch->fetch_all(MYSQLI_ASSOC));//获取全部数据
  7. print_r($res_fetch->fetch_row());//获取一条数据,返回索引数组
  8. $res_fetch->fetch_array(MYSQLI_ASSOC);//每次获取一行,指针下移
  9. //循环打印:
  10. while($res = $res_fetch->fetch_array(MYSQLI_ASSOC)){
  11. print_r($res);
  12. }
  13. //更新数据
  14. $sql = "update `goods` set `status`=1 where id<4";
  15. $mysqli->query($sql);
  16. echo '受影响记录数:'.$mysqli->affected_rows;//受影响记录数:3
  17. //删除数据
  18. $sql = "delete from `goods` where id<5";
  19. $mysqli->query($sql);
  20. echo '受影响记录数:'.$mysqli->affected_rows;//受影响记录数:4
  21. //关闭连接
  22. mysqli_close($mysqli);
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:代码整洁规范, 不错的, 少了一个总结
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!