mysql怎么修改成mysqli链接数据库? 5
<?phpif(!defined('IN_DANG')){exit('AccessDenied');}require_once(DANG_ROOT.'include/db...
<?php
if (!defined('IN_DANG')){
exit('Access Denied');
}
require_once (DANG_ROOT.'include/db_config.php');
class MySqlClass{
private $db_server;
private $db_username;
private $db_pass;
private $db_name;
function __construct($db_server,$db_username,$db_pass,$db_name){
$this->db_server=$db_server;
$this->db_username=$db_username;
$this->db_pass=$db_pass;
$this->db_name=$db_name;
}
function conn_select(){
$link=$this->connect();
$conn=$this->select();
$this->query("SET NAMES 'GBK'");
}
function connect(){
return mysql_connect($this->db_server,$this->db_username,$this->db_pass) or die ('数据库链接错误,请检查数据库用户名、密码、服务器地址是否填写正确!'.$this->show_error());
}
function select(){
return mysql_select_db($this->db_name) or die('链接数据库表: \"'.$this->db_name.'\" 错误!'.'<br />'.$this->show_error());
}
private function show_error() {
return '错误信息:'.$this->error().'<br />'.'错误代码:'.$this->errno();
}
private function error() {
return mysql_error();
}
private function errno() {
return mysql_errno();
}
function query($sql) {
if(!($query = mysql_query($sql))){
echo '错误sql语句:'.$sql.'<br />'.$this->show_error();
exit();
}
return $query;
}
function fetch_frist($query){
return $this->fetch_array($this->query($query));
}
function num_rows($query) {
return @mysql_num_rows($query);
}
function fetch_array($query) {
return mysql_fetch_array($query);
}
function insert_id() {
return mysql_insert_id();
}
function version() {
return mysql_get_server_info();
}
function close() {
return mysql_close();
}
}
$db = new MySqlClass(DB_SERVER,DB_USERNAME,DB_PASS,DB_NAME);
?> 展开
if (!defined('IN_DANG')){
exit('Access Denied');
}
require_once (DANG_ROOT.'include/db_config.php');
class MySqlClass{
private $db_server;
private $db_username;
private $db_pass;
private $db_name;
function __construct($db_server,$db_username,$db_pass,$db_name){
$this->db_server=$db_server;
$this->db_username=$db_username;
$this->db_pass=$db_pass;
$this->db_name=$db_name;
}
function conn_select(){
$link=$this->connect();
$conn=$this->select();
$this->query("SET NAMES 'GBK'");
}
function connect(){
return mysql_connect($this->db_server,$this->db_username,$this->db_pass) or die ('数据库链接错误,请检查数据库用户名、密码、服务器地址是否填写正确!'.$this->show_error());
}
function select(){
return mysql_select_db($this->db_name) or die('链接数据库表: \"'.$this->db_name.'\" 错误!'.'<br />'.$this->show_error());
}
private function show_error() {
return '错误信息:'.$this->error().'<br />'.'错误代码:'.$this->errno();
}
private function error() {
return mysql_error();
}
private function errno() {
return mysql_errno();
}
function query($sql) {
if(!($query = mysql_query($sql))){
echo '错误sql语句:'.$sql.'<br />'.$this->show_error();
exit();
}
return $query;
}
function fetch_frist($query){
return $this->fetch_array($this->query($query));
}
function num_rows($query) {
return @mysql_num_rows($query);
}
function fetch_array($query) {
return mysql_fetch_array($query);
}
function insert_id() {
return mysql_insert_id();
}
function version() {
return mysql_get_server_info();
}
function close() {
return mysql_close();
}
}
$db = new MySqlClass(DB_SERVER,DB_USERNAME,DB_PASS,DB_NAME);
?> 展开
1个回答
展开全部
您好,mysqli有2种编写方法,一种是面向过程,一种是面向对象。
官网上面有面向对象的写法,您可以到php官网参考一下。
mysqli的面向过程基本上和mysql的思路差不多,也是先连接,选择数据库,查询,取结果...
$sql = "SELECT * FROM table";
/* 连接 */
$con = mysqli_connect("localhost","user","password","dbname");
if (!$con)
{
die("连接错误: " . mysqli_connect_error());
}
/* 查询 */
$Result = mysqli_query($con,$sql);
/* 遍历结果集 */
while($data = mysqli_fetch_assoc($Result))
{
$Data_Username = $data["username"];
$Data_Password = $data["password"];
echo "用户名:{$Data_Username},密码:{$Data_Password}";
}
/* 释放查询结果 */
mysqli_free_result($Result);
/* 关闭连接 */
mysqli_close($con);
官网上面有面向对象的写法,您可以到php官网参考一下。
mysqli的面向过程基本上和mysql的思路差不多,也是先连接,选择数据库,查询,取结果...
$sql = "SELECT * FROM table";
/* 连接 */
$con = mysqli_connect("localhost","user","password","dbname");
if (!$con)
{
die("连接错误: " . mysqli_connect_error());
}
/* 查询 */
$Result = mysqli_query($con,$sql);
/* 遍历结果集 */
while($data = mysqli_fetch_assoc($Result))
{
$Data_Username = $data["username"];
$Data_Password = $data["password"];
echo "用户名:{$Data_Username},密码:{$Data_Password}";
}
/* 释放查询结果 */
mysqli_free_result($Result);
/* 关闭连接 */
mysqli_close($con);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |