PHP连接MySQL

数据库

假设数据库为以下:

主机名 库名 用户名 密码 端口
localhost demo demo 123456 3306
1. PHP连接数据库
<?php
//$con =mysqli_connect(主机名,用户名,密码,库名,端口);
$con =mysqli_connect('localhost','demo','123456','demo','3306');
?>
2. 执行SQL语句
<?php
mysqli_query($con,$sql); 
//$con为连接的数据库
//$sql为要执行的SQL语句,每次一条
?>
3. 设置编码
mysqli_query($con,"set sql_mode = ''");
mysqli_query($con,"set character set 'utf8'");
mysqli_query($con,"set names 'utf8'");
4. 关闭数据库连接
<?php
mysqli_close($con);
?>

原文地址:https://blog.lylme.com/archives/phptomysql.html

posted @ 2022-04-30 17:16  六零。  阅读(54)  评论(0)    收藏  举报