検索
ホームページphp教程php手册php4およびphp5のmysqlデータベース操作クラスをサポート

php4およびphp5のmysqlデータベース操作クラスをサポート

Jun 13, 2016 pm 12:30 PM
mysqlphp5使用フロントエンド操作するサポートデータベース親切

フロントエンドは PHP5 を使用していましたが、これを仮想ホストで実行するには、PHP4 に変更する必要があります。以前にこれらのライブラリ クラスを PHPCHIAN に投稿しました。アドレスは http://www.phpchina.com/bbs/viewthread.php?tid=5687&highlight= です。 (数日前にネットで検索したら、私の記事の多くが出典を明記せずに転載され、著作権が削除されていたことが分かり、とても腹が立ちました。)

昨日、データベース操作クラスを書き換えました。これは、zend フレームワークを単純化するときにたまたま使用されました。

代码如下:


/**
* ファイル名: DB_Mysql.class.php
* @package:phpbean
* @author :feifengxlq
* @copyright :Copyright 2006 feifengxlq
* @license:version 1.2
* create:2006-5-30
* modify:2006-10-19 by feifengxlq
* description:the mysqlのインターフェース。
*
* 例:
* ////////////アクションの選択(最初のモード)////////////////// ////////////
$mysql=new DB_Mysql("localhost","root","root","root");    
$rs=$mysql->query("select * from test");
for($i=0;$inum_rows($rs);$i )
$record[$i]=$mysql->seek($i);
print_r($record);
$mysql->close();
* ////////////アクションの選択(2 番目のモード)//////////////////////////// //
$mysql=new DB_Mysql("localhost","root","root","root");
$rs=$mysql->execute("select * from test");
print_r($rs);
$mysql->close();
* /////////////アクションの挿入////////////////////////////
$mysql=new DB_Mysql("localhost","root","root","root");    
$mysql->query("insert into test(username) values('test from my DB_mysql')");
printf("%s",$mysql->insert_id());
$mysql->close();
*/
class mysql{

/* private: connectionパラメータ */
var $host="localhost";
var $database="";
var $user="root";
var $password="";

/* private: 構成パラメータ */
var $pconnect=false;
var $debug=false;

/* private: 結果の配列と現在の行番号 */
var $link_id=0;
var $query_id=0;
var $record=array();

/**
* construct
*
* @param string $host
* @param string $user
* @param string $password
* @param string $database
    */
function __construct($host="localhost",$user="root",$password="",$database="")
{
$this->set("host",$host);
$this->set("user",$user);
$this->set("パスワード",$パスワード);
$this->set("データベース",$database);
$this->connect();
}

/**
* このクラスのパラメータの値を設定します
*
* @param string $var
* @param string $value
    */
function set($var,$value)
{
$this->$var=$value;
}


/**
* mysql サーバーに接続し、データベースを選択します。
*
* @param string $database
* @param string $host
* @param string $user
* @param string $password
* @return link_id
    */
function connect($database="",$host="",$user="",$password="")
{
if(!empty($database))$this->set("database",$database);
if(!empty($host))$this->set("host",$host);
if(!empty($user))$this->set("user",$user);
if(!empty($password))$this->set("password",$password);
if($this->link_id==0)
{
if($this->pconnect)
$this->link_id=@mysql_pconnect($this->host ,$this->ユーザー,$this->パスワード);
else
$this->link_id=@mysql_connect($this->host,$this->user,$this->password);
if(!$this->link_id)
die("Mysql Connect Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
if(!@mysql_select_db($this->database,$this->link_id))
die("Mysql Select database Error in ".__FUNCTION__."():".mysql_errno()." :".mysql_error());
}
return $this->link_id;
}

/**
* データベースに SQL をクエリします
*
* @param string $strsql
* @return query_id
    */
function query($strsql="")
{
if(empty($strsql)) die("Mysql Error :".__FUNCTION__."() strsql は空です!");
if($this->link_id==0) $this->connect();
if($this->debug) printf("デバッグクエリ sql:%s",$strsql);
$this->query_id=@mysql_query($strsql,$this->link_id);
if(!$this->query_id) die("Mysql query fail,Invalid sql:".$strsql.".");
return $this->query_id;
}

/**
* データベースに SQL をクエリしますが、query() メソッドとは異なります。
* このメソッドはレコード (配列) を返します。
*
* @param string $strsql
* @param string $style
* @return $record is a array()
    */
function Execute($strsql,$style="array")
{
$this->query($strsql);
if(!empty($this->record))$this->record=array();
$i=0;
if($style=="array"){
while ($temp=@mysql_fetch_array($this->query_id)) {
$this->record[$i]=$temp ;
$i ;
}
} else { while($ temp =@mysql_fetch_object($ this-> query_id)){
$ this-> record [$ i] = $ temp;
$i ;
}
}
unset($i);
unset($temp);
$this-> レコードを返す;
}

/**
* seek ですが、mysql_data_seek と等しくありません。 このメソッドはリストを返します。
*
* @param int $pos
* @param string $style
* @return record
    */
関数 seek($pos=0,$style="array")
{
if(!@mysql_data_seek($ this->query_id,$pos))
die("Error in".__FUNCTION__."():can not seek to row ".$pos."!");
$result=@($style=="array")?mysql_fetch_array($this->query_id):mysql_fetch_object($this->query_id);
if(!$result) die("「.__FUNCTION__."(): データを取得できません!」);
$result を返す;
}
/**
* クエリの結果を無料
*
    */
function free()
{
if(($this->query_id)&($this->query_id!=0 ))@mysql_free_result($this->query_id);
}

/**
* 結果 (サイズ、幅) を評価
*
* @return num
    */
function affected_rows()
{
return @mysql_affected_rows($this->link_id);
}

function num_rows()
{
return @mysql_num_rows($this->query_id);
}

function num_fields()
{
return @mysql_num_fields($this->query_id);
}

function insert_id()
{
return @mysql_insert_id($this->link_id);
}

function close()
{
$this->free();
if($this->link_id!=0)@mysql_close($this->link_id);
if(mysql_errno()!=0) die("Mysql Error:".mysql_errno().":".mysql_error());
}

function select($strsql,$number,$offset)
{
if(empty($number)){
return $this->Execute($ strsql);
else{
return $this->Execute($strsql.' limit '.$offset.','.$number);
}
}

function __destruct()
{
$this->close();
$this->set("ユーザー","");
$this->set("ホスト","");
$this->set("パスワード","");
$this->set("データベース","");
}
}
?> 



このベースでは、SIDU (select、insert、update、delete) という 4 つの基本操作を、Zend Framework の標準化されたモジュールとしてパッケージ化しています。了、懒的写本。):



クラス モジュール{

var $mysql;

var $tbname;

var $debug=false;

function __construct($tbname){
if(!is_string($tbname))die('モジュールにはテーブル名の引数が必要');
$this->tbname=$tbname;
$this->mysql=phpbean::registry('db');
}

function _setDebug($debug=true){
$this->debug=$debug;
}

function add($row){
if(!is_array($row))die('モジュールエラー:行は配列でなければなりません');
$strsql='insert into `'.$this->tbname.'`';
$keys='';
$values='';
foreach($row as $key=>$value){
$keys.='`'.$key.'`,';
$values.='''.$value.''';
}
$keys=rtrim($keys,',');
$values=rtrim($values,',');
$strsql.=' ('.$keys.') values ('.$values.')';
if($this->debug)echo '


'.$strsql.'
';
$this->mysql->query($strsql);
return $this->mysql->insert_id();
}

function query($strsql){
return $this->mysql->Execute($strsql);
}

function count($where=''){
$strsql='select count(*) as num from `'.$this->tbname.'` ';
if(!empty($where))$strsql.=$where;
$rs=$this->mysql->Execute($strsql);
return $rs[0]['num'];
}

function select($where=''){
$strsql='select * from `'.$this->tbname.'` ';
if(!empty($where))$strsql.=$where;
return $this->mysql->Execute($strsql);
}

function delete($where=''){
if(empty($where))die('エラー: 削除メソッドには条件が必要です!');
return $this->mysql->query('delete from `'.$this->tbname.'` '.$where);
}

function update($set,$where){
if(empty($where))die('エラー: 更新メソッドには条件が必要です!');
if(!is_array($set))die('エラー: セットは配列である必要があります!');
$strsql='update `'.$this->tbname.'` ';
//セットの文字列を取得します
$strsql.='set ';
foreach($set as $key=>$value){
$strsql.='`'.$key.'`=''.$value.'',';
}
$strsql=rtrim($strsql,',');
return $this->mysql->query($strsql.' '.$where);
}

function detail($where){
if(empty($where))die('エラー:where を空にするべきではありません!');
$rs=$this->mysql->query('select * from `'.$this->tbname.'` '.$where);
return $this->mysql->seek(0);
}
}
?>

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。