php4 および php5_PHP チュートリアルの mysql データベース操作クラスのサポート
フロントエンドは PHP5 を使用していましたが、これを仮想ホストで実行するには、PHP4 に変更する必要があります。以前にこれらのライブラリ クラスを PHPCHIAN に投稿しました。アドレスは http://www.phpchina.com/bbs/viewthread.php?tid=5687&highlight= です。 (数日前にネットで検索したら、私の記事が出典を明記せずに転載され、著作権が削除されていることが多くて、とても腹が立ちました。)
昨日、データベース操作クラスを書き換えたのですが、たまたまそうなりました。簡易zendフレームワークも使用できます。
代码如下:
/**
* ファイル名: DB_Mysql.class.php
* @package:phpbean
* @author :feifengxlq<[email]feifengxlq@gmail.com[/email]>
* @copyright :Copyright 2006 feifengxlq
* @license:version 1.2
* create:2006-5-30
* modify:2006-10-19 by feifengxlq
* description: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("ローカルホスト","ルート","ルート","ルート");
$mysql->query("insert into test(username) values('test from my DB_mysql')");
printf("%s",$mysql->insert_id());
$mysql->close();
*/
class mysql{
/* private: 接続パラメータ */
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);
$this->set("user",$user);
$this->set("パスワード",$パスワード);
$this->set("データベース",$database);
$this->connect();
}
/**
* この クラスの param の値を設定します
*
* @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);
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(".__FUNCTION__.でMysql接続エラーが発生しました。"():".mysql_errno().":".mysql_error()); if(!@mysql_select_db($ this-&gt; database、$ this-&gt; link_id))
die( "mysql select database error in" .__ function __。 "():"。mysql_errno()。 ":"。 ());
}
return $this->link_id;
}
/**
* データベースに SQL をクエリします
*
* @param string $strsql
* @return query_id
*/
function query($strsql="")
{
if(empty($strsql)) die("Mysql Error:".__FUNCTION__."() strsql is empty! ");
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 は 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
*/
function seek($pos=0,$style="array")
{
if(!@mysql_data_seek($this->query_id,$pos))
die (".__FUNCTION__."() のエラー:行 ".$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->クエリ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);
}
関数 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);
}
}
関数__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('エラー:Set は配列である必要があります!');
$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);
}
}
?>

ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









MySQLは、インストールが簡単で、強力で管理しやすいため、初心者に適しています。 1.さまざまなオペレーティングシステムに適した、単純なインストールと構成。 2。データベースとテーブルの作成、挿入、クエリ、更新、削除などの基本操作をサポートします。 3.参加オペレーションやサブクエリなどの高度な機能を提供します。 4.インデックス、クエリの最適化、テーブルパーティション化により、パフォーマンスを改善できます。 5。データのセキュリティと一貫性を確保するために、バックアップ、リカバリ、セキュリティ対策をサポートします。

次の手順でphpmyadminを開くことができます。1。ウェブサイトコントロールパネルにログインします。 2。phpmyadminアイコンを見つけてクリックします。 3。MySQL資格情報を入力します。 4.「ログイン」をクリックします。

NAVICATプレミアムを使用してデータベースを作成します。データベースサーバーに接続し、接続パラメーターを入力します。サーバーを右クリックして、[データベースの作成]を選択します。新しいデータベースの名前と指定された文字セットと照合を入力します。新しいデータベースに接続し、オブジェクトブラウザにテーブルを作成します。テーブルを右クリックして、データを挿入してデータを挿入します。

MySQLは、オープンソースのリレーショナルデータベース管理システムです。 1)データベースとテーブルの作成:createdatabaseおよびcreateTableコマンドを使用します。 2)基本操作:挿入、更新、削除、選択。 3)高度な操作:参加、サブクエリ、トランザクション処理。 4)デバッグスキル:構文、データ型、およびアクセス許可を確認します。 5)最適化の提案:インデックスを使用し、選択*を避け、トランザクションを使用します。

MySQLとSQLは、開発者にとって不可欠なスキルです。 1.MYSQLはオープンソースのリレーショナルデータベース管理システムであり、SQLはデータベースの管理と操作に使用される標準言語です。 2.MYSQLは、効率的なデータストレージと検索機能を介して複数のストレージエンジンをサポートし、SQLは簡単なステートメントを通じて複雑なデータ操作を完了します。 3.使用の例には、条件によるフィルタリングやソートなどの基本的なクエリと高度なクエリが含まれます。 4.一般的なエラーには、SQLステートメントをチェックして説明コマンドを使用することで最適化できる構文エラーとパフォーマンスの問題が含まれます。 5.パフォーマンス最適化手法には、インデックスの使用、フルテーブルスキャンの回避、参加操作の最適化、コードの読み取り可能性の向上が含まれます。

手順に従って、NAVICATで新しいMySQL接続を作成できます。アプリケーションを開き、新しい接続(CTRL N)を選択します。接続タイプとして「mysql」を選択します。ホスト名/IPアドレス、ポート、ユーザー名、およびパスワードを入力します。 (オプション)Advanced Optionsを構成します。接続を保存して、接続名を入力します。

NAVICATでSQLを実行する手順:データベースに接続します。 SQLエディターウィンドウを作成します。 SQLクエリまたはスクリプトを書きます。 [実行]ボタンをクリックして、クエリまたはスクリプトを実行します。結果を表示します(クエリが実行された場合)。

データベースに接続するときの一般的なエラーとソリューション:ユーザー名またはパスワード(エラー1045)ファイアウォールブロック接続(エラー2003)接続タイムアウト(エラー10060)ソケット接続を使用できません(エラー1042)SSL接続エラー(エラー10055)接続の試みが多すぎると、ホストがブロックされます(エラー1129)データベースは存在しません(エラー1049)
