PHP入门教程之使用Mysqli操作数据库的方法(连接(2)

<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //数据库连接时发生的错误 if (mysqli_connect_errno()) { echo '数据库连接出现了错误.错误的信息是:'.mysqli_connect_error(); exit(); } //设置一下编码 $_mysqli->set_charset('utf8'); //创建一句SQL,获取数据库的表的数据 $_sql = "SELECT * FROM tg_user"; //创建一个结果集 $_result = $_mysqli->query($_sql); //使用OOP的方法object //print_r($_result->fetch_object()); echo $_result->fetch_object()->tg_username; //使用OOP遍历 while (!!$_object = $_result->fetch_object()) { echo $_object->tg_username.'<br />'; } $_mysqli->close(); ?>

Demo10.php

<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //数据库连接时发生的错误 if (mysqli_connect_errno()) { echo '数据库连接出现了错误.错误的信息是:'.mysqli_connect_error(); exit(); } //设置一下编码 $_mysqli->set_charset('utf8'); //创建一句SQL,获取数据库的表的数据 $_sql = "SELECT * FROM tg_user limit 0,10"; //创建一个结果集 $_result = $_mysqli->query($_sql); //我要看下我选择了多少行 echo $_result->num_rows; //我影响了多少行呢 echo $_mysqli->affected_rows; $_mysqli->close(); ?>

Demo11.php

<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //数据库连接时发生的错误 if (mysqli_connect_errno()) { echo '数据库连接出现了错误.错误的信息是:'.mysqli_connect_error(); exit(); } //设置一下编码 $_mysqli->set_charset('utf8'); //创建一句SQL,获取数据库的表的数据 $_sql = "UPDATE tg_user SET tg_username='一站式建网站' WHERE tg_id=5"; //创建一个结果集 $_result = $_mysqli->query($_sql); //我要看下我选择了多少行 echo $_result->num_rows; echo '|'; //我影响了多少行呢 echo $_mysqli->affected_rows; $_mysqli->close(); ?>

Demo12.php

<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //数据库连接时发生的错误 if (mysqli_connect_errno()) { echo '数据库连接出现了错误.错误的信息是:'.mysqli_connect_error(); exit(); } //设置一下编码 $_mysqli->set_charset('utf8'); //创建一句SQL,获取数据库的表的数据 $_sql = "SELECT * FROM tg_user"; //创建一个结果集 $_result = $_mysqli->query($_sql); //求出表中有多少个字段 echo $_result->field_count; //获取字段的名字 // $_field = $_result->fetch_field(); // echo $_field->name; // $_field = $_result->fetch_field(); // echo $_field->name; // // while (!!$_field = $_result->fetch_field()) { // echo $_field->name.'<br />'; // } //一次性取得所有的字段 $_fields = $_result->fetch_fields(); //echo $_fields[0]->name; foreach ($_fields as $_field) { echo $_field->name.'<br />'; } $_mysqli->close(); ?>

Demo13.php

<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //数据库连接时发生的错误 if (mysqli_connect_errno()) { echo '数据库连接出现了错误.错误的信息是:'.mysqli_connect_error(); exit(); } //设置一下编码 $_mysqli->set_charset('utf8'); //创建一句SQL,获取数据库的表的数据 $_sql = "SELECT * FROM tg_user"; //创建一个结果集 $_result = $_mysqli->query($_sql); //移动数据指针 $_result->data_seek(9); $_row = $_result->fetch_row(); echo $_row[3]; //移动字段指针 $_result->field_seek(3); $_field = $_result->fetch_field(); echo $_field->name; $_mysqli->close(); ?>

Demo14.php

<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //数据库连接时发生的错误 if (mysqli_connect_errno()) { echo '数据库连接出现了错误.错误的信息是:'.mysqli_connect_error(); exit(); } //设置一下编码 $_mysqli->set_charset('utf8'); //创建三个修改的SQL语句 $_sql .= "UPDATE tg_article SET tg_username='喀喀喀' WHERE tg_id=1;"; $_sql .= "UPDATE tg_flower SET tg_fromuser='喀喀喀' WHERE tg_id=1;"; $_sql .= "UPDATE tg_friend SET tg_fromuser='喀喀喀' WHERE tg_id=1"; //使用通知执行的方法 $_mysqli->multi_query($_sql); //普通只能执行sql的方法是:$_mysqli->query($_sql); $_mysqli->close(); ?>

Demo15.php

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/0632d89f1666874860594a7fc904804b.html