案例1:查询select
使用php连接数据库class9,
获取数据库的表student中的信息,
然后输出到页面上(用表格套住)
 <?php
header("Content-type:text/html; charset=utf-8");
// 建立mysql的链接
// 参数:主机名,用户,密码,需要的数据库
/*
在PHP函数方法前面加@符号,表示忽略警告
*/
$conn =@mysqli_connect("localhost","root","","lanou0322");
// 判断数据库链接是否成功
/*if($conn){
echo "成功了!";
}else{
echo "失败了!";
}
*/
if(!$conn){
echo "失败";
// 终止
exit;
}
$conn->query('set names utf8');
$sql ="SELECT * FROM student";
$result = $conn->query($sql);
// 5.判断
// mysqli_num_rows 返回的条数
// echo mysqli_num_rows($result);
if(mysqli_num_rows($result)>0){
echo "<table border=1>";
echo "<tr>
<th>id</th>
<th>name</th>
<th>sex</th>
<th>age</th>
</tr>";
while($row = $result -> fetch_assoc()){
echo "<tr>
<td style='width:20'>{$row['id']}</td>
<td style='width:80; text-align:center;'>{$row['name']}</td>
<td style='width:20'>{$row['sex']}</td>
<td style='width:20'>{$row['age']}</td>
</tr>";
}
echo "</table>";
//如果想在一个页面输出同样的两个数据库表格那么可以在执行一次$conn->query($sql);然后打印
echo "<hr/>";
$result = $conn->query($sql);
echo "<table border=1>";
echo "<tr>
<th>id</th>
<th>name</th>
<th>sex</th>
<th>age</th>
</tr>";
while($row = $result -> fetch_assoc()){
echo "<tr>
<td style='width:20'>{$row['id']}</td>
<td style='width:80; text-align:center;'>{$row['name']}</td>
<td style='width:20'>{$row['sex']}</td>
<td style='width:20'>{$row['age']}</td>
</tr>";
}
echo "</table>";
}
// 关闭数据库
$conn->close();//关闭数据库 ?>
效果:
案例2:插入insert

使用php连接数据库

1. 向数据表student,插入一条信息:"小明", 28, "男"   添加成功输出提示"添加成功"

 
2. 插入多条信息
 <?php
header("Content-type:text/html;charset=utf-8");
//链接数据库
$conn =@mysqli_connect("localhost","root","","lanou0322");
if(!$conn){
echo "失败";
// 终止
exit;
}
$conn->query('set names utf8');
//向数据库添加数据INSERT INTO name(数据表名) VALUE (添加的数据信息);
$sql ="INSERT INTO student(name,sex,age) VALUES ('小黄','女','17')";
$sql ="SELECT * FROM student";
$result = $conn->query($sql);
// 判断插入是否成功
// 使用 mysqli_affected_rows($conn);
if(mysqli_affected_rows($conn)>0){
echo "成功";
}else{
echo "失败";
}
echo mysqli_num_rows($result);
if(mysqli_num_rows($result)>0){
echo "<table border=1>";
echo "<tr>
<th>id</th>
<th>name</th>
<th>sex</th>
<th>age</th>
</tr>";
while($row = $result -> fetch_assoc()){
echo "<tr>
<td style='width:20'>{$row['id']}</td>
<td style='width:80; text-align:center;'>{$row['name']}</td>
<td style='width:20'>{$row['sex']}</td>
<td style='width:20'>{$row['age']}</td>
</tr>";
}
echo "</table>";
}
$conn->close();//关闭数据库
?>
效果:
案例3:更新update
使用php连接数据库
更新(修改)数据表student中,年龄age=28的改为10
 <?php
header("Content-type:text/html;charset=utf-8");
$conn =@mysqli_connect("localhost","root","","lanou0322");
$conn->query('set names utf8');
// mysqli_query($conn,"UPDATA name SET... WHERE ...")
$sql = mysqli_query($conn,"UPDATE student SET name = '我傻逼',age = '10' WHERE id = 1");
$sql ="SELECT * FROM student";
$result = $conn->query($sql);
echo mysqli_affected_rows($conn);
// 判断修改是否成功
if(mysqli_affected_rows($conn)>0){
echo "成功";
}else{
echo "失败";
}
echo mysqli_num_rows($result);
if(mysqli_num_rows($result)>0){
echo "<table border=1>";
echo "<tr>
<th>id</th>
<th>name</th>
<th>sex</th>
<th>age</th>
</tr>";
while($row = $result -> fetch_assoc()){
echo "<tr>
<td style='width:20'>{$row['id']}</td>
<td style='width:80; text-align:center;'>{$row['name']}</td>
<td style='width:20'>{$row['sex']}</td>
<td style='width:20'>{$row['age']}</td>
</tr>";
}
echo "</table>";
}
$conn->close();
?>
效果:

案例4:删除delete
使用php连接数据库class9,
删除数据库的表student中,id号为1的数据
 <?php
header("Content-type:text/html;charset=utf-8");
$conn =@mysqli_connect("localhost","root","","lanou0322");
$conn->query('set names utf8');
$sql = mysqli_query($conn,"DELETE FROM student WHERE id = 1");
$sql ="SELECT * FROM student";
$result = $conn->query($sql);
// echo mysqli_affected_rows($conn);
// 判断修改是否成功
if(mysqli_affected_rows($conn)>0){
echo "成功";
}else{
echo "失败";
}
// echo mysqli_num_rows($result);
if(mysqli_num_rows($result)>0){
echo "<table border=1>";
echo "<tr>
<th>id</th>
<th>name</th>
<th>sex</th>
<th>age</th>
</tr>";
while($row = $result -> fetch_assoc()){
echo "<tr>
<td style='width:20'>{$row['id']}</td>
<td style='width:80; text-align:center;'>{$row['name']}</td>
<td style='width:20'>{$row['sex']}</td>
<td style='width:20'>{$row['age']}</td>
</tr>";
}
echo "</table>";
}
$conn->close();
?>

效果:

 

关于PHP数据库mysql的一些案例的更多相关文章

  1. 阿里云资深DBA专家罗龙九:云数据库十大经典案例分析【转载】

    阿里云资深DBA专家罗龙九:云数据库十大经典案例分析 2016-07-21 06:33 本文已获阿里云授权发布,转载具体要求见文末 摘要:本文根据阿里云资深DBA专家罗龙九在首届阿里巴巴在线峰会的&l ...

  2. 数据库MySQL学习笔记高级篇

    数据库MySQL学习笔记高级篇 写在前面 学习链接:数据库 MySQL 视频教程全集 1. mysql的架构介绍 mysql简介 概述 高级Mysql 完整的mysql优化需要很深的功底,大公司甚至有 ...

  3. 记一次血淋淋的MySQL崩溃修复案例

    摘要:今天给大家带来一篇MySQL数据库崩溃的修复案例 本文分享自华为云社区<记一次MySQL崩溃修复案例,再也不用删库跑路了>,作者: 冰 河. 问题描述 研究MySQL源代码,调试并压 ...

  4. MYSQL添加新用户 MYSQL为用户创建数据库 MYSQL为新用户分配权限

    1.新建用户 //登录MYSQL @>mysql -u root -p @>密码 //创建用户 mysql> insert into mysql.user(Host,User,Pas ...

  5. Robot Framework-DatabaseLibrary数据库(MySql)

    Robot Framework-Mac版本安装 Robot Framework-Windows版本安装 Robot Framework-工具简介及入门使用 Robot Framework-Databa ...

  6. paip.导入数据英文音标到数据库mysql为空的问题之道解决原理

    paip.导入数据英文音标到数据库mysql为空的问题之道解决原理 #---原因:mysql 导入工具的bug #---解决:使用双引号不个音标括起来. 作者 老哇的爪子 Attilax 艾龙,  E ...

  7. paip.解决 数据库mysql增加列 字段很慢添加字段很慢

    paip.解决 数据库mysql增加列 字段很慢添加字段很慢 #环境如下: mysql5.6    数据仅仅3w alter table xxx add column yyy int default ...

  8. Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql、oracle...)间进行数据的传递

    http://niuzhenxin.iteye.com/blog/1706203   Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql.postgresql.. ...

  9. 数据库MySQL多个数据库服务冲突

    一.目标名称 MySQL 二.目标版本 mysql-5.6.24-win32.1432006610.zip 三.环境信息 系统:windows 7 旗舰版 防火墙:关闭  —— 注意:如果防火墙不关闭 ...

随机推荐

  1. C#注册表操作类(完整版) 整理完整

    /// <summary> /// 注册表基项静态域 /// /// 主要包括: /// 1.Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT主键 /// ...

  2. 3:C#异步WaitAll的使用

    编写界面如图: private async void button1_Click(object sender, EventArgs e) { #region 单个执行的异步,效率慢 HttpClien ...

  3. MysqliDb 库的一些使用简单技巧(php)

    一.分页功能 假设接口要接受输入:page, page_limit,key,value,table 来查询 table 中 key like value 的元组中以 page_limit 为 page ...

  4. matlab中显示灰阶图像

    matlab的数据源文件中400张图片,每张图片是一个112*92的矩阵表示,而400张图片存储在一个cell数组ime中,显示第一张图片,指令是: colormap(gray) imagesc(im ...

  5. springboot多数据源动态切换和自定义mybatis分页插件

    1.配置多数据源 增加druid依赖 完整pom文件 数据源配置文件 route.datasource.driver-class-name= com.mysql.jdbc.Driver route.d ...

  6. declare命令

    还是围绕以下几个问题进行学习; 1.declare是什么? 2.问什么要用declare? 3.怎样使用declare? 1.declare是什么? ♦declare应用的很多,向我们各种语言都会有声 ...

  7. SpringData JPA实现CRUD,分页与多参数排序

    Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数据访问计数,包括非关系数据库.Map-Reduce 框架.云数据服务等等,SpringData JPA是简化创建 JPA ...

  8. Markdown数学公式速查记录

    参考: Markdown数学公式语法 markdown最全数学公式速查 行内与独行 行内公式:将公式插入到本行内,符号:$公式内容$,如:$xyz$ 独行公式:将公式插入到新的一行内,并且居中,符号: ...

  9. MyBatisSystemException->BindingException: Parameter 'xxx' not found. Available parameters are [arg1, arg0, param1, param2]

    最近在使用Spring boot+mybatis做新的基础框架,结果碰到如下问题: 1 org.mybatis.spring.MyBatisSystemException: nested except ...

  10. jedis 连接redis

    一,  单机版连接 @Test public void testJedis() { //1. 创建jedis 对象 Jedis jedis = new Jedis("192.168.88.1 ...