赞
踩
PHP 5 之前使用mysql扩展连接数据库
因为电脑上安装的PHP是7.x因此是出现如下报错,PHP5.x mysql_connect和mysqli_connect都可以使用
PHP5 之后使用一下方式连接MySQL
MySQLi extension ("i" 意为 improved)
PDO (PHP Data Objects)
mysqli_connect():连接数据库
mysqli_select_db():更改数据库
mysqli_close():关闭数据库连接
mysqli_query():执行数据语句
mysqli_num_rows():返回select查询了多少几条数据
mysqli_afferted_rows($con):返回受影响的条数
插入数据
- <?php
- $con=mysqli_connect('192.168.138.129','root','shy014');
- mysqli_select_db($con,'sql');//更改选择数据库
- if (!$con) {
- die("无法连接到数据库服务器");
- }
- print("连接成功");
- echo PHP_EOL;
- //往数据库内插入数据
- $sql="insert into users(id,username,address) values(4,'苏七','意大利')";
- $result=mysqli_query($con,$sql);
- if ($result) {
- echo "插入成功";
- }else{
- echo "插入失败";
- }
- mysqli_close($con);
-
- ?>
更新数据:
- <?php
- $con=mysqli_connect('192.168.138.129','root','shy014');
- mysqli_select_db($con,'sql');//更改选择数据库
- if (!$con) {
- die("无法连接到数据库服务器");
- }
- print("连接成功");
- echo PHP_EOL;
- //往数据库内插入数据
- $sql="insert into users(id,username,address) values(9,'苏七','意大利')";
- $result=mysqli_query($con,$sql);
- if ($result) {
- echo "插入成功";
- }else{
- echo "插入失败";
- }
- echo PHP_EOL;
- //往数据库内更新数据
- $up="update users set username='苏二十七' where username='苏七'";
- $result=mysqli_query($con,$up);
- if ($result) {
- echo "更新成功";
- }else{
- echo "更新失败";
- }
- print_r($result);
- echo PHP_EOL;
- mysqli_close($con);
- ?>
上述代码将插入与更新数据库语句的执行结果保存到了变量$result中,此时变量$result为布尔类型,语句执行成功$result=1为true;语句执行不成功,$result=0;为false;
获取查询(select)结果的条数,只对select语句有关系。
- <?php
- $con=mysqli_connect('192.168.138.129','root','shy014');
- mysqli_select_db($con,'sql');//更改选择数据库
- if (!$con) {
- die("无法连接到数据库服务器");
- }
- print("连接成功");
- echo PHP_EOL;
- $sel="select * from users";
- $result=mysqli_query($con,$sel);
- $num=mysqli_num_rows($result);
- echo "查询结果有".$num."条记录。";
- ?>
返回前一次mysql操作受影响的行数
affect | 英[əˈfekt] | 美[əˈfekt] |
v. | 影响; 侵袭; 使感染; (感情上) 深深打动; 使悲伤(或怜悯等); |
- <?php
- $con=mysqli_connect('192.168.138.129','root','shy014');
- mysqli_select_db($con,'sql');//更改选择数据库
- if (!$con) {
- die("无法连接到数据库服务器");
- }
- print("连接成功");
- echo PHP_EOL;
- $sel="select * from users";
- $result=mysqli_query($con,$sel);
- $num=mysqli_num_rows($result);
- echo "查询结果有".$num."条记录。";
- $insert="insert into users(id,username,address) values(12,'张三十一','四川')";
- $result1=mysqli_query($con,$insert);
- if (!$result1) {
- echo "插入失败";
- }
- $numin=mysqli_affected_rows($con);
- echo "本次插入数据影响了".$numin."行。";
- ?>
从select查询结果中获取一条数据并把它变成数组保存,如果想全部取出,可以结合循环语句。
- <?php
- $con=mysqli_connect('192.168.138.129','root','shy014');
- mysqli_select_db($con,'sql');//更改选择数据库
- if (!$con) {
- die("无法连接到数据库服务器");
- }
- print("连接成功");
- echo PHP_EOL;
- $sel="select * from users";
- $result=mysqli_query($con,$sel);
- $num=mysqli_num_rows($result);
- echo "查询结果有".$num."条记录。";
- while($row=mysqli_fetch_row($result))
- {
- print_r($row);
- }
- ?>
运行结果
获取查询结果中的条数作用关联数组,返回的是关联数组,而且区分大小写
- while($row=mysqli_fetch_assoc($result))
- {
- print_r($row);
- }
从结果中获取一行记录作为对象
- while($row=mysqli_fetch_object($result))
- {
- echo $row->id."和".$row->username;
- echo PHP_EOL;
- }
获取查询结果并将结果保存为数组。result_type是常量,是可选参数。
MYSQL_ASSOC(关联数组) MYSQL_NUM(数字数组)MYSQL_BOTH(两者都有)(默认)
- while($row=mysqli_fetch_array($result))
- {
- echo $row[0]."和".$row["id"];
- echo PHP_EOL;
- }
释放资源,释放SQL请求所返回的对象$result所占用的资源。
- <!DOCTYPE html>
- <html>
- <head>
- <title>select test</title>
- </head>
- <h2>user find test </h2>
- <body>
- <form action="select.php" method="post">
- full find user:
- <input type="text" name="username" size="20"><br />
- <input type="submit" name="submit" value="find"><br />
- </form>
-
- </body>
- </html>
浏览器效果
- <!DOCTYPE html>
- <html>
- <head>
- <title>User found</title>
- </head>
- <body>
- <h2>User found from mysql database</h2>
- <?php
- $username=$_POST['username'];
- if (!$username) {
- echo "ERROR:this is no username";
- exit;
- }
- if (!get_magic_quotes_gpc())
- {
- $username=addslashes($username);
- }
- @ $con=mysqli_connect('localhost','root','root','sql');
- if (mysqli_connect_errno()) {
- echo "error:could not connect to mysql";
- }
-
- $sql="select * from users where username='$username'";
- echo "sql语句是:".$sql."<br />";
- echo PHP_EOL;
- $result=mysqli_query($con,$sql);
- $rownum=mysqli_num_rows($result);
- echo "影响条数:".$rownum."<br />";
- for ($i=0; $i <$rownum; $i++) {
- $row=mysqli_fetch_assoc($result);
- echo "ID:".$row['id']."<br />";
- echo "名字:".$row['username']."<br />";
- echo "地址:".$row['address']."<br />";
- }
- mysqli_free_result($result);
- mysqli_close($con);
- ?>
- </body>
- </html>
浏览器运行结果:
insert.html
- <!DOCTYPE html>
- <meta charset="utf-8">
- <html>
- <head>
- <title>insert message</title>
- </head>
- <body>
- <h2>insert message</h2>
- <form action="insert.php" method="post">
- select gender:
- <select name="gender">
- <option value="male">man</option>
- <option value="female">female</option>
- </select><br>
- fill user name:
- <input type="text" name="name"><br>
- fill user address:
- <input type="text" name="address"><br>
- <input type="submit" name="submit" value="提交">
- </form>
- </body>
- </html>
insert.php
- <!DOCTYPE html>
- <html>
- <head>
- <title>insert </title>
- </head>
- <body>
- <h2>display insert</h2>
- <?php
- $gender=$_POST["gender"];
- $name=$_POST["name"];
- $address=$_POST["address"];
- $con=mysqli_connect('localhost','root','root');
- mysqli_select_db($con,'sql');
- if (!$con) {
- echo "数据库连接失败:".mysqli_errno();
- }
- $sql="insert into users(id,username,address,gender) values(13,'$name','$address','$gender')";
- //echo "SQL语句是:".$sql;
- echo $sql;
- $result=mysqli_query($con,$sql);
- $resultnum=mysqli_affected_rows($con);
- if (!$result) {
- echo "插入失败";
- }else
- {
- echo "插入成功";
- }
- mysqli_close($con);
- ?>
- </body>
- </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。