PHP与MySQL被称为“黄金组合”?四种连接方法,你学会了吗?
PHP支持多种数据库,像IBM 的DB2、Informix、oracle、SQL Sever、MySQL、Sybase等等。Mysql是瑞典AB公司旗下所开发的一款大众实用的数据库管理系统。2008年1月16日被Sun公司收购。而2009年,Sun又被Oracle收购。Mysql是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。


作为一个小型关系型数据库管理系统,PHP与Mysql被称为黄金组合。
MySQL 采用了GPL(GNU通用公共许可证)。由于体积小,速度快、总体拥有成本低,尤其是具有开放源码这一特点 ,许多中小型网站为了降低网站总体拥有成本而选择了MySQL 作为网站数据库。而使用MySQL数据库管理系统与PHP脚本语言相结合的数据库管理系统与PHP脚本语言相结合的数据库系统解决方案,正被越来越多的网站所采用,其中又以LAMP模式最为流行.

下面小编给大家分享php连接Mysql的三种不同方法.
方法一:
<?php
$servername = "localhost";//mysql服务器主机地址
$username = "root"; //mysql用户名
$password = "111";//mysql用户名密码
// 创建连接
$conn = new mysqli($servername, $username, $password);
// 检测连接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
方法二:
<?php
header("Content-Type: text/html; charset=UTF-8");
$host = "127.0.0.1";//mysql服务器主机地址
$userName = "root"; //mysql用户名
$passWord = "111";//用户密码
$dbName = "user";//数据库
$connID = mysqli_connect($host,$userName,$passWord);//创建连接
if(mysqli_select_db($connID,$dbName)){
echo "数据库连接成功";
}else{
echo "数据库连接失败";
}
方法三:
<?php
$dbhost = 'localhost'; //mysql服务器主机地址
$dbuser = 'root'; //mysql用户名
$dbpass = '111';//mysql用户名密码
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($conn);
?>
方法四:
<?php
$con = mysql_connect('localhost','root','111');
mysql_query('set names utf8',$con);
mysql_query('use user',$con);
//使用数据库
$sql ='insert into student(uid,boy,girl) values(04,"aa","nnn")';
$res = mysql_query($sql);
var_dump($res);
相关推荐
-
PHP8种变量类型的详细讲解2025-02-22 00:32:24
-
php+apache 和 php+nginx的区别2025-02-22 00:21:27
-
PHP:与workerman结合实现定时任务2025-02-22 00:15:57
-
Nginx的Rewrite规则与实例2025-02-22 00:15:39
-
MySql中身份证字段的简单脱敏介绍2025-02-22 00:15:36