<?php
$con = mysql_connect("localhost","root","12345"); $dbcharset = "utf8";
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary");
if (!$con)
{
die('Could not connect: ' . mysql_error());
} // some code mysql_select_db("my_db", $con);
?>

1.php连接数据库

$mysqli = mysqli_connect("hostname", "username", "password", "database");

<?php
$mysqli = mysqli_connect("localhost", "root", "12345", "test");
//错误信息
if(mysqli_connect_errno()) {
//错误信息
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
//输出Host信息
printf("Host infomation: %s\n", mysqli_get_host_info($mysqli));
//显示关闭连接
mysqli_close($mysqli);
}
?>

旧版使用mysql函数集

2.查询

<?php
$mysqli = mysqli_connect("localhost", "root", "12345", "test");
//错误信息
if(mysqli_connect_errno()) {
//错误信息
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$sql = "CREATE TABLE testTable (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, testField VARCHAR(75))";
$res = mysqli_query($mysqli, $sql);
if($res === TRUE) {
echo "Table testTable successfully created.";
} else {
//获取MYSQL错误信息
printf("Could not create table: %s\n", mysqli_error($mysqli));
}
//显示关闭连接
mysqli_close($mysqli);
}
?>
mysqli_error($mysqli)可以获取错误信息

3.mysqli_query使用Mysql数据

3.1避免SQL注入

在一个登陆过程中,查询过程大概如下

SELECT * FROM users WHERE name = '".$_POST['username']."';

假设username的值为 ' or  '1' = '1

这会产生一个完整的查询如

SELECT * FROM users Where name = '  '  or  '1' = 1;

这个查询结果总是为真。 这就是sql注入。

要避免注入就要限制用户的输入,比如特殊字符(and, or)等都禁止提交。

3.2插入数据

插入表单中的数据

表单提交页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<form method="post" action="insert.php" >
<input type="text" name='testfield' />
<input type="submit" />
</form>
<body>
</body>
</html>

处理页面insert.php

<?php
$mysqli = mysqli_connect("localhost", "root", "12345", "test");
//错误信息
if(mysqli_connect_errno()) {
//错误信息
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
// 超全局变量$_POST访问表单
$clean_text = mysqli_real_escape_string($mysqli, $_POST['testfield']);
$sql = "INSERT INTO testTable (testField)
VALUES ('".$clean_text."')";
$res = mysqli_query($mysqli, $sql);
if($res === TRUE) {
echo "插入数据成功";
} else {
//获取MYSQL错误信息
printf("插入数据失败: %s\n", mysqli_error($mysqli));
}
//显示关闭连接
mysqli_close($mysqli);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
</body>
</html>

3.3php获取数据

3.31查询结果个数

<?php
$mysqli = mysqli_connect("localhost", "root", "12345", "test");
//错误信息
if(mysqli_connect_errno()) {
//错误信息
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$sql = "SELECT * FROM testTable";
$res = mysqli_query($mysqli, $sql);
if($res) {
//mysqli_num_rows获取结果个数
$number_of_rows = mysqli_num_rows($res);
printf("拥有%s组结果:", $number_of_rows);
} else {
//获取MYSQL错误信息
printf("查询失败: %s\n", mysqli_error($mysqli));
}
//mysqli_free_result释放结果内存供其它脚本使用
mysqli_free_result($res);
//显示关闭连接
mysqli_close($mysqli);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
</body>
</html>
mysqli_free_result释放结果内存供其它脚本使用,mysqli_num_rows获取结果个数

3.32显示查询结果

<?php
$mysqli = mysqli_connect("localhost", "root", "12345", "test");
//错误信息
if(mysqli_connect_errno()) {
//错误信息
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$sql = "SELECT * FROM testTable";
$res = mysqli_query($mysqli, $sql);
if($res) {
//mysqli_fetch_array获取每一行构成的数组
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
$id = $newArray['id'];
$testField = $newArray['testField'];
echo "第".$id."条的内容为".$testField."。";
}
} else {
//获取MYSQL错误信息
printf("查询失败: %s\n", mysqli_error($mysqli));
}
//mysqli_free_result释放结果内存供其它脚本使用
mysqli_free_result($res);
//显示关闭连接
mysqli_close($mysqli);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
</body>
</html>

更多的MySQLi部分http://www.php.net/mysqli

PHP与MySQL交互的更多相关文章

  1. mysql交互协议解析——mysql包基础数据、mysql包基本格式

    mysql交互协议是开发mysql周边组件常用的协议,如JDBC,libmysql等等. 在此我们要认识到mysql交互协议其实是半双工的交互协议,至于为什么,这里就先挖个小坑,以后再填. 在探讨my ...

  2. Nodejs学习笔记(四)--- 与MySQL交互(felixge/node-mysql)

    目录 简介和安装 测试MySQL 认识一下Connection Options MYSQL CURD 插入 更新 查询 删除 Nodejs 调用带out参数的存储过程,并得到out参数返回值 结束数据 ...

  3. Shell基础:Shell和Mysql交互

    通过命令行和Mysql交互 [root]#mysql -uroot -p123 -e "show databases"   -e: execute: 执行数据库命令 通过脚本和数据 ...

  4. Hadoop集群(第10期)_MapReduce与MySQL交互

    2.MapReduce与MySQL交互 MapReduce技术推出后,曾遭到关系数据库研究者的挑剔和批评,认为MapReduce不具备有类似于关系数据库中的结构化数据存储和处理能力.为此,Google ...

  5. 与MySQL交互(felixge/node-mysql)

    目录 简介和安装 测试MySQL 认识一下Connection Options MYSQL CURD 插入 更新 查询 删除 Nodejs 调用带out参数的存储过程,并得到out参数返回值 结束数据 ...

  6. Nodejs学习笔记(四)—与MySQL交互(felixge/node-mysql)

    简介和安装 Node.js与MySQL交互操作有很多库,具体可以在 https://www.npmjs.org/search?q=mysql  查看. 我选择了felixge/node-mysql,用 ...

  7. python3与mysql交互:pymysql

    python3与mysql交互 1.安装pymysql模块 pip3 install pymysql3 2.pymysql的简单使用: # /usr/bin/env python3 import py ...

  8. 第一节、Alex 讲解 python+mysql 交互;

    Python Mysql 交互 A.Alex 的语法展示: import MySQLdb  try:      conn=MySQL.connect(host='localhost',user='ro ...

  9. Python与Mysql交互

    #转载请联系 在写内容之前,先放一张图,bling- 这张图算是比较详细的表达出了web开发都需要什么.用户访问网页,就是访问服务器的网页文件.这些网页文件由前端工程师编写的.服务器通常用nginx/ ...

  10. 【04】【转】Nodejs学习笔记(四)--- 与MySQL交互(felixge/node-mysql)

    目录 简介和安装 测试MySQL 认识一下Connection Options MYSQL CURD 插入 更新 查询 删除 Nodejs 调用带out参数的存储过程,并得到out参数返回值 结束数据 ...

随机推荐

  1. OpenStack core components CLI快速调用API

    1,openStack core components CLI 使用自身参数执行;

  2. UVA 562 Dividing coins(dp + 01背包)

    Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...

  3. sysbench的安装与使用

    sysbench是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试.数据库目前支持MySQL/Oracle/PostgreSQL 安装过程(rhel5.8+mys ...

  4. 数据结构multiset hdu-2275-Kiki & Little Kiki 1

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2275 题目意思: 有两种操作: 1.push a 把a放进数组里. 2.pop a 输出不超过a的最 ...

  5. [poj 1144]Network[Tarjan求割点]

    题意: 求一个图的割点. 输入略特别: 先输入图中点的总数, 接下来每一行首先给出一个点u, 之后给出一系列与这个点相连的点(个数不定). 行数也不定, 用0作为终止. 这样的输入还是要保证以数字读入 ...

  6. z-index解决弹出层遮罩层覆盖子div不能显示输出的问题

    // 添加以下代码来进行测试: // ajax 发生错误,就会执行$('body').ajaxError(function(e, xhr, setting, text){    // e - even ...

  7. (转)python struct简介

    最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概了解了,在这里做一下简单的总结. 了解c语言 ...

  8. C语言_double_精度的谜团

    double-long long 和0的比较,double和double之间比较

  9. linux下的gdb调试工具--断点调试

    到目前为止我们的调试手段只有一种: 根据程序执行时的出错现象假设错误原因,然后在代码中适当的位置插入printf,执行程序并分析打印结果,如果结果和预期的一样,就基本上证明了自己假设的错误原因,就可以 ...

  10. PCL点云库增加自定义数据类型

    #include <pcl/filters/passthrough.h> #include <pcl/filters/impl/passthrough.hpp> // the ...