PHP method to connect to mysql database

墨辰丷
Release: 2023-03-27 20:06:02
Original
2590 people have browsed it

This article mainly introduces the steps of connecting PHP to mysql database. It has a very good reference value. Let’s take a look at it with the editor.

Connect to the database:

Use Object-oriented method;

1. Create a mysqli object and a connection object

2. Prepare a SQL statement

3. Execute the SQL statement, if it is a query statement. Successfully return results and objects

4. Find data from the result set object;

Query attributes Code:

<?php
//造一个mysqli对象,造连接对象
$db=new MySQLi("localhost","root","12345678","heiheihei");
//括号里面填的是IP地址域名,用户名,密码,数据库的名字
  //准备一条SQL语句
$sql = "select * from student";
//执行SQL语句,如果是查询语句。成功返回结果及对象
$reslut = $db->query($sql);
//判断返回是否有内容,执行成功
if($reslut)
{
//如果成功了,从结果集对象里找到数据;
  $attr = $reslut->fetch_all();
//返回索引数组:$attr = $reslut->fetch_row()
//返回关联数组:tch_assoc();
//返回对象:$attr = $reslut->fetch_object();
//返回既有索引又有关联:$arrt = $reslut->fetch_array();
  var_dump($attr);
?>
Copy after login

The final return value is a set object. In order to see the results clearly, we use fetch_all(), fetch_row() ; fetch_object();

Add, delete and modify statements:

Add a piece of information:

//造连接对象
$db = new MySQLi("localhost","root","12345678","heiheihei");
//准备SQL语句
$sql = "insert into cou values(&#39;3-111&#39;,&#39;嘿嘿&#39;,&#39;803&#39;)";
  //执行SQL语句
$r = $db ->query($sql);
if($r)//如果$r是ture
{
  echo "执行成功";
}
else {
  echo "执行失败";
}
Copy after login

Change a message:

<?php
//造连接对象
$db = new MySQLi("localhost","root","12345678","heiheihei");
//准备SQL语句
$sql = "update cou set cname=&#39;哈哈&#39; where tno=&#39;803&#39;";
  //执行SQL语句
$r = $db ->query($sql);
if($r)
{
  echo "执行成功";
}
else {
  echo "执行失败";
}
?>
Copy after login

Delete:

<?php
//造连接对象
$db = new MySQLi("localhost","root","12345678","heiheihei");
//准备SQL语句
$sql = "delete from cou where tno=&#39;803&#39;";
  //执行SQL语句
$r = $db ->query($sql);
if($r)
{
  echo "执行成功";
}
else {
  echo "执行失败";
}
?>
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php PDO implements method to determine whether connection is available

Detailed interpretation of the shared database method of connecting between Node.js modules (graphic tutorial)

php implements mysqlConnection Detailed explanation of pool effect steps

The above is the detailed content of PHP method to connect to mysql database. For more information, please follow other related articles on 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!