Home > Backend Development > PHP Tutorial > PHP introductory tutorial: Analysis of how to operate MySQL with PHP

PHP introductory tutorial: Analysis of how to operate MySQL with PHP

高洛峰
Release: 2023-03-03 21:54:01
Original
926 people have browsed it

The example in this article describes how to operate MySQL with PHP. Share it with everyone for your reference, the details are as follows:

Set all UTF-8 encoding of Zend software

设置Zend 软件所有 UTF-8 编码

Set the UTF-8 encoding of a single project

设置Zend 软件所有 UTF-8 编码

Demo1.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  //第一步,连接到 Mysql 服务器 3306
  //第二步参数,服务器地址;第二个参数,服务器的用户名;第三个参数,服务器密码
  //@ 如果出错了,不要出现警告或错误,直接忽略
  //die 函数之前,先连接一下,报错流程
  //echo (!!mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;123456&#39;));
// if(!$conn = @mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;123456&#39;)){
//   echo &#39;数据库连接失败,错误信息&#39;.mysql_error();
//   exit;
// }
// echo $conn;
// echo &#39;连接成功了,我才能显示!&#39;;
  //常量参数
  define(&#39;DB_HOST&#39;,&#39;localhost&#39;);
  define(&#39;DB_USER&#39;,&#39;root&#39;);
  define(&#39;DB_PWD&#39;,&#39;123456&#39;);
  define(&#39;DB_NAME&#39;,&#39;school&#39;);
  //第一步,连接数据库
  //mysql_connect -- 打开一个到 MySQL 服务器的连接
  $conn = @mysql_connect(DB_HOST,DB_USER,DB_PWD) or die(&#39;数据库连接失败,错误信息&#39;.mysql_error());
  //第二步,选择指定的数据库,设置字符集
  //mysql_select_db -- 选择 MySQL 数据库
  mysql_select_db(DB_NAME) or die (&#39;数据库错误,错误信息:&#39;.mysql_error());
  mysql_query(&#39;SET NAMES UTF8&#39;)or die(&#39;字符集设置错误,错误信息&#39;.mysql_error());
  //第三步,从这个数据库里选一张表(grade),然后把这个表的数据库提出(获取记录集)
  $query = "SELECT * FROM grade";
  //mysql_query -- 发送一条 MySQL 查询
  $result = mysql_query($query) or die (&#39;SQL错误:&#39;.mysql_error());
  //$result 就是记录集
  //第四步,将记录集里的数据显示出来
  print_r(mysql_fetch_array($result,MYSQL_NUM));//按照数字下标来显示
  //print_r(mysql_fetch_array($result,MYSQL_ASSOC)); //按照字符串下标来显示
  print_r(mysql_fetch_array($result,MYSQL_NUM));
  print_r(mysql_fetch_array($result,MYSQL_NUM));
  //第五步,释放记录集资源
  //mysql_free_result -- 释放结果内存
  mysql_free_result($result);
  //最后一步:关闭数据库
  //mysql_close -- 关闭 MySQL 连接
  echo mysql_close();
?>
Copy after login

config. php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  //常量参数
  define(&#39;DB_HOST&#39;,&#39;localhost&#39;);
  define(&#39;DB_USER&#39;,&#39;root&#39;);
  define(&#39;DB_PWD&#39;,&#39;123456&#39;);
  define(&#39;DB_NAME&#39;,&#39;school&#39;);
  //第一步,连接MYSQL 服务器
  $conn = @mysql_connect(DB_HOST,DB_USER,DB_PWD) or die(&#39;数据库连接失败,错误信息&#39;.mysql_error());
  //第二步,选择指定的数据库,设置字符集
  mysql_select_db(DB_NAME) or die (&#39;数据库错误,错误信息:&#39;.mysql_error());
  mysql_query(&#39;SET NAMES UTF8&#39;)or die(&#39;字符集设置错误,错误信息&#39;.mysql_error());
?>
Copy after login

Demo2.php

<?php
  require &#39;config.php&#39;;
  //新增数据
// $query = "INSERT INTO grade (
//   name,
//   email,
//   point,
//   regdate)
// VALUES (
//   &#39;景临境&#39;,
//   &#39;jly@163.com&#39;,
//   &#39;78&#39;,
//   NOW()
// )";
  //$query = "INSERT INTO grade (name,email,point,regdate) VALUES (&#39;与共&#39;,&#39;abc@163.com&#39;,&#39;78&#39;,NOW() )";
  //mysql_query($query) or die(&#39;新增错误:&#39;.mysql_error());
  //修改数据
// $query = &#39;UPDATE grade SET point=66 WHERE id = 7&#39;;
// @mysql_query($query) or die(&#39;修改失败:&#39;.mysql_error());
  //删除数据
// $query = &#39;DELETE FROM grade WHERE id= 4&#39;;
// @mysql_query($query) or die(&#39;删除失败:&#39;.mysql_error());
  //显示数据
  $query = &#39;SELECT id,name,email FROM grade &#39;;
  $result = mysql_query($query) or die(&#39;SQL 语句有误:&#39;.mysql_error());
// $row = mysql_fetch_array($result);
// echo $row[2];
// $row = mysql_fetch_array($result);
// echo $row[2];
  //把结果集转换成数组赋给 $row ,如果有数据,就为真
  while (!!$row = mysql_fetch_array($result)){
    echo $row[&#39;id&#39;].&#39;----&#39;.$row[&#39;name&#39;].&#39;-----&#39;.$row[&#39;email&#39;];
    echo &#39;<br/>&#39;;
  }
  mysql_close();
?>
Copy after login

Demo3.php

<?php
  require &#39;config.php&#39;;
  //显示数据
  $query = &#39;SELECT id,email,name FROM grade &#39;;
  $result = mysql_query($query) or die(&#39;SQL 语句有误:&#39;.mysql_error());
// print_r(mysql_fetch_array($result));
// print_r(mysql_fetch_array($result,MYSQL_ASSOC));
// print_r(mysql_fetch_row($result));
// print_r(mysql_fetch_assoc($result));
// while (!!$row = mysql_fetch_array($result)){
//   echo $row[&#39;id&#39;].&#39;----&#39;.$row[&#39;name&#39;].&#39;-----&#39;.$row[&#39;email&#39;];
//   //print_r(mysql_fetch_lengths($result));
//   echo mb_strlen($row[&#39;name&#39;],&#39;utf-8&#39;);
//   echo &#39;<br/>&#39;;
// }
  //echo mysql_field_name($result,2); //name
  //echo mysql_num_fields($result); //3
  for($i=0; $i<mysql_num_fields($result);$i++){
    echo mysql_field_name($result,$i); //id----email----name----
    echo &#39;----&#39;;
  }
  echo &#39;<br/>&#39;;
  echo mysql_num_rows($result); //求出多少条数据
  echo &#39;<br/>&#39;;
  echo mysql_get_client_info();//取得 MySQL 客户端信息 //5.0.51a
  echo &#39;<br/>&#39;;
  echo mysql_get_host_info();//取得 MySQL 主机信息 //localhost via TCP/IP
  echo &#39;<br/>&#39;;
  echo mysql_get_proto_info();//取得 MySQL 协议信息 //10
  echo &#39;<br/>&#39;;
  echo mysql_get_server_info();//取得 MySQL 服务器信息 //5.0.51b-community-nt-log
  mysql_close();
?>
Copy after login

For more PHP introductory tutorials and PHP operation MySQL method analysis related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template