mysqli:
<?php
$servername = "localhost"
$username = "username"
$password = "password"
// 创建连接
$conn = new mysqli($servername, $username, $password)
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error)
}
echo "连接成功"
?>
也可以使用PDO进行链接,前提是你必须在php.ini中开启PDO:
<?php
$servername = "localhost"
$username = "username"
$password = "password"
try {
$conn = new PDO("mysql:host=$servernamedbname=myDB", $username, $password)
echo "连接成功"
}
catch(PDOException $e)
{
echo $e->getMessage()
}
?>
建议使用PDO,功能更加强大,兼容各种数据库
关于这个问题,差不多就是这个样子的了,你如果不明白,可以自己去后盾瞅瞅,我这些都是在后盾上学的,有空可以去看一下,就算不喜欢也没关系啊,何乐而不为呢?
//链接数据库$db=mysql_connect("localhost", "root","123456")
$sqlname="database"
mysql_select_db($sqlname,$db)
session_start()
mysql_query("SET NAMES 'utf8'",$db)
?>
<?
//创建结果集
$sql = "SELECT * FROM kecheng"
$result = mysql_query($sql)
$rs= mysql_fetch_array($result)
?>
<?
//修改数据
$sql = "UPDATE news SET title='1111' where news_id=1"
$result = mysql_query($sql)
?>
<?
//删除数据
$sql = "DELETE from news where news_id=1"
$result = mysql_query($sql)
?>
<?
//添加数据
$sql = "INSERT INTO news (title,fenshu) VALUES ('a',1)"
$result = mysql_query($sql)
?>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)