php连接数据库

<?php
header('Content-Type:text/html; charset=utf-8'); define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PWD', '密码');
define('DB_NAME', '数据库名'); $conn = @mysql_connect(DB_HOST, DB_USER, DB_PWD) or die('数据库链接失败:'.mysql_error()); @mysql_select_db(DB_NAME) or die('数据库错误:'.mysql_error()); @mysql_query('SET NAMES UTF8') or die('字符集错误:'.mysql_error());
?>
/*****************************
*数据库连接
*****************************/ header("Content-Type:text/html;charset=utf-8");
$conn = @mysql_connect("localhost","root","123456");
if (!$conn){
die("连接数据库失败:" . mysql_error());
}
mysql_select_db("数据库名", $conn);
//字符转换,读库
mysql_query("set character set 'utf-8'");
//写库
mysql_query("set names 'utf-8'"); mysql_query("create table user
(id int(10) not null auto_increment,
name varchar(30) CHARACTER SET utf8 not null DEFAULT '',
password varchar(30) not null,
age int(2),
address varchar(30),
primary key(id)
)ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
");
 <?php
require 'config.php';
$_pass = sha1($_POST['login_pass']);
$query = mysql_query("SELECT user,pass FROM user WHERE
user='{$_POST['login_user']}' AND pass='{$_pass}'") or die('SQL 错误!');
if (mysql_fetch_array($query, MYSQL_ASSOC)) {
echo 'true';
} else {
echo 'false';
}
mysql_close();
?>
上一篇:Linux下解压文件


下一篇:Istio 运维实战系列(2):让人头大的『无头服务』-上