一介简介的Mysql封装类
mysql|封装
class_mysql.php
//######################################################################
//##### TITLE :: CLASS MYSQL
//##### FILE :: class_mysql.php
//##### PROJECT :: WebVision
//##### RELATED DOCUMENT :: None
//##### DESCRIPTION ::
//##### To provide access utility for MySQL access
//##### RunDB is used to run SQL query with the result
//##### grouped into array.
//##### AUTHOR :: Mark Quah
//##### REVISION ::
//######################################################################
class MYSQL
{
var $no_rows=0, $row=array();
var $no_fields=0, $field=array();
//#-----------------------------------------------------------------
//#---- FUNCTION :: MYSQL($p_host, $p_user, $p_passwd, $p_db="mysql")
//#---- DESCRIPTION ::
//#---- Initialize class with information to access server
//#---- No connection will be made at this point.
//#---- INPUT ::
//#---- p_host : server hostname|IP address
//#---- p_user : user name to log into server
//#---- p_passwd : passwd for the user
//#---- p_db : database to be used
//#---- OUTPUT ::
//#---- none
//#-----------------------------------------------------------------
function MYSQL($p_host, $p_user, $p_passwd, $p_db="mysql")
{
$this->sql_host = $p_host;
$this->sql_user= $p_user;
$this->sql_passwd = $p_passwd;
$this->sql_db = $p_db;
} // end MYSQL
//#-----------------------------------------------------------------
//#---- FUNCTION :: RunDB($statement, $exp_result = "")
//#---- DESCRIPTION ::
//#---- Execute a MySQL statement in a non-persistant mode
//#---- INPUT ::
//#---- p_statement : statement to be executed
//#---- exp_result : is result expected?
//#---- value 1 (default): result stored in row array
//#---- value 0: result not stored in row array
//#---- OUTPUT ::
//#---- return "OK" : succesful
//#---- return err_message from mysql_connect
//#---- exp_result==1: additional result stored into array row
//#---- no_row contains no. of record retrieved
//#---- row[recno][ "field" ] contains value of recno record
//#---- field["fieldname"] contains the field list
//#-----------------------------------------------------------------
function RunDB($p_statement, $exp_result = 1)
{
//--- Connect to the Database
$link=mysql_connect($this->sql_host, $this->sql_user, $this->sql_passwd);
if (!$link)
return sprintf("error connecting to host %s, by user %s",
$this->sql_host, $this->sql_user) ;
//--- Select the Database
if (!mysql_select_db($this->sql_db, $link))
{ $err_msg=sprintf("Error in selecting %s database",
$this->sql_db);
$err_msg .= sprintf("error:%d %s", mysql_errno($link),
mysql_error($link));
return $err_msg;
}
//--- Execute the Statement
if (!($this->result=mysql_query($p_statement, $link)))
{ $err_msg=sprintf("Error in selecting %s database\n",
$this->sqldb);
$err_msg .= sprintf("\terror:%d\t\nerror message %s",
mysql_errno($link), mysql_error($link));
return $err_msg;
}
//--- Organize the result
if ($exp_result == 1)
{ $this->no_rows = mysql_num_rows($this->result);
$this->GroupResult();
}
//--- SUCCESS RETURN
return OK;
} // end function RunDB
//#-----------------------------------------------------------------
//#---- FUNCTION :: GroupResult( )
//#---- DESCRIPTION ::
//#---- To group the raw result retrieved in an associative array
//#---- A query has to be made using RunDB prior to this execution
//#---- The handle is storedin $result
//#---- INPUT :: None
//#---- OUTPUT :
//#---- return none
//#---- additional result stored into array
//#---- no_row, row[recno]["field"] = value
//#---- no_field, field["fieldname"]
//#-----------------------------------------------------------------
function GroupResult()
{
//--- Get RESULT
$is_header = FALSE;
for ( $recno = 0; $recno no_rows; $recno ++ )
{ $row = mysql_fetch_object($this->result);
//--- Get Field List
if ( ! $is_header )
{ $no_fields = 0;
$t_row = $row;
while ( $item = each($t_row) )
{ $this->field[$no_fields] = $item["key"];
$no_fields ++;
}
$this->no_fields = $no_fields;
$is_header = TRUE;
}
//---- GET DATA
while ( $item = each($row))
$this->row[$recno][$item["key"]] = $item["value"];
}
//--- END CONNECTION
mysql_free_result($this->result);
} // GroupResult
//#-----------------------------------------------------------------
//#---- FUNCTION :: ShowHTML($p_table="", $p_header = "", $p_cell = "")
//#---- DESCRIPTION ::
//#---- To return the result in HTML Table format
//#---- INPUT ::
//#---- p_table : HTML
recno"; for ($i = 0; $i no_fields; $i ++) printf(" |
%s", $this->field[$i]); //--- DISPLAY DATA for ( $i = 0; $i no_rows; $i ++) { echo " |
%-3s", $i); for ($f = 0; $f no_fields; $f ++) { $f_name = $this->field[$f]; $f_value = $this->row[$i][$f_name]; if ( $f_value=="" ) $f_value=" "; printf(" |
%s", $f_value); } } //--- THE END echo " |
} // ShowHTML
} // end class MYSQL
?>
例子:
include("class_mysql.php");
//===== set up sql connection
$mysql=new MYSQL("server", "mysql userid", "mysql passwd", "mysql DB");
//==== Extract Result
$status = $mysql->RunDB("select * from user;");
if ($status != "OK")
{ echo "
DB Error: $status.
";
die;
}
for ($i = 0; $i no_rows; $i ++)
{
echo "Record No: " . ($i + 1) ."
";
for ($j = 0; $j no_fields; $j ++)
{
$field_name = $mysql->field[$j];
echo "Field: ".$field_name." ----- Value: ".
$mysql->row[$i][$field_name]."
";
}
}
//==== Use the built-in ShowHTML format
$status = $mysql->RunDB("select * from user;");
if ($status != "OK")
{ echo "
DB Error: $status.
";
die;
}
$mysql->ShowHTML("","","CENTER");
//==== Run some query not expecting results
$stmt = ("FILL IN YOUR STAEMENT eg. INSERT INTO");
$status = $myql->RunDB($stmt, 0);
if ($status
if ($status != "OK")
{ echo "
DB Fail: $status.
";
die;
}
else
{ echo "
Success: $status.
";
die;
}
?>

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MySQL在Web應用中的主要作用是存儲和管理數據。 1.MySQL高效處理用戶信息、產品目錄和交易記錄等數據。 2.通過SQL查詢,開發者能從數據庫提取信息生成動態內容。 3.MySQL基於客戶端-服務器模型工作,確保查詢速度可接受。

在 Docker 中啟動 MySQL 的過程包含以下步驟:拉取 MySQL 鏡像創建並啟動容器,設置根用戶密碼並映射端口驗證連接創建數據庫和用戶授予對數據庫的所有權限

Laravel 是一款 PHP 框架,用於輕鬆構建 Web 應用程序。它提供一系列強大的功能,包括:安裝: 使用 Composer 全局安裝 Laravel CLI,並在項目目錄中創建應用程序。路由: 在 routes/web.php 中定義 URL 和處理函數之間的關係。視圖: 在 resources/views 中創建視圖以呈現應用程序的界面。數據庫集成: 提供與 MySQL 等數據庫的開箱即用集成,並使用遷移來創建和修改表。模型和控制器: 模型表示數據庫實體,控制器處理 HTTP 請求。

在開發一個小型應用時,我遇到了一個棘手的問題:需要快速集成一個輕量級的數據庫操作庫。嘗試了多個庫後,我發現它們要么功能過多,要么兼容性不佳。最終,我找到了minii/db,這是一個基於Yii2的簡化版本,完美地解決了我的問題。

優雅安裝 MySQL 的關鍵在於添加 MySQL 官方倉庫。具體步驟如下:下載 MySQL 官方 GPG 密鑰,防止釣魚攻擊。添加 MySQL 倉庫文件:rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm更新 yum 倉庫緩存:yum update安裝 MySQL:yum install mysql-server啟動 MySQL 服務:systemctl start mysqld設置開機自啟動

在 CentOS 上安裝 MySQL 涉及以下步驟:添加合適的 MySQL yum 源。執行 yum install mysql-server 命令以安裝 MySQL 服務器。使用 mysql_secure_installation 命令進行安全設置,例如設置 root 用戶密碼。根據需要自定義 MySQL 配置文件。調整 MySQL 參數和優化數據庫以提升性能。

文章摘要:本文提供了詳細分步說明,指導讀者如何輕鬆安裝 Laravel 框架。 Laravel 是一個功能強大的 PHP 框架,它 упростил 和加快了 web 應用程序的開發過程。本教程涵蓋了從系統要求到配置數據庫和設置路由等各個方面的安裝過程。通過遵循這些步驟,讀者可以快速高效地為他們的 Laravel 項目打下堅實的基礎。

MySQL与其他编程语言相比,主要用于存储和管理数据,而其他语言如Python、Java、C 则用于逻辑处理和应用开发。MySQL以其高性能、可扩展性和跨平台支持著称,适合数据管理需求,而其他语言在各自领域如数据分析、企业应用和系统编程中各有优势。
