好的,这是我正在研究的功能。现在请注意,我是编程新手,然后我可能会有更多错误。基本上,我如何设置变量:
${"MySQL_Connection_" . $MySQL_Connection}
当我回想起具有不同“模式”的功能时使用它。我尝试过使用:
static
但是从你们所说的话来看。我对其使用情况严重误导。这是我的“真实生活”例子:
<?php
/*
DB_Core_("","","","","");
DB_Close
DB_Core_("DB_Close",<$MySQL_Connection>);
DB_Connection_Status
DB_Core_("DB_Connection_Status",<$MySQL_Connection>);
DB_Connection_Status_Report_Short
DB_Core_("DB_Connection_Status_Report_Short");
DB_Connection_Status_Report_Long
DB_Core_("DB_Connection_Status_Report_Long");
MySQL_Query
DB_Core_("MySQL_Query",
"
"
);
MySQL_Query_Status
DB_Core_("MySQL_Query_Status",<$MySQL_Connection>);
MySQL_Query_Status_Report_Short
DB_Core_("MySQL_Query_Status_Report_Short");
MySQL_Query_Status_Report_Long
DB_Core_("MySQL_Query_Status_Report_Short");
MySQL_Connect
DB_Core_("MySQL_Connect",<$MySQL_Connection>,
<$Database_Server_Name>,
<$Database_Server_Username>,
<$Database_Server_Username_Password>
);
*/
function DB_Core_ ($DB_Core_Command = "Empty", // Default if not set
$DB_Core_Command_Variable_1 = "Empty", // Default if not set
$DB_Core_Command_Variable_2 = "Empty", // Default if not set
$DB_Core_Command_Variable_3 = "Empty", // Default if not set
$DB_Core_Command_Variable_4 = "Empty" // Default if not set
){
// Variables
$MySQL_Connection_Prefix = "MySQL_Connection_";
// ********************
// Set Function Mode(s)
// ********************
switch ($DB_Core_Command) {
// ****************************************
// Function to end connection to a database
// ****************************************
case "DB_Close":
$MySQL_Connection = $DB_Core_Command_Variable_1;
mysqli_close(${"MySQL_Connection_" . $MySQL_Connection});
break; // END Function (DB_Close)
// *****************************************************
// Function to check the connection status of a database
// *****************************************************
case "DB_Connection_Status":
$MySQL_Connection = $DB_Core_Command_Variable_1;
if (!${"MySQL_Connection_" . $MySQL_Connection}) {
$DB_Connection_Status_Short = 0;
$DB_Connection_Status_Long = "Connection failed: " . mysqli_connect_error();
die();
} else {
$DB_Connection_Status_Short = 1;
$DB_Connection_Status_Long = "Connected successfully";
}
break; // END Function (DB_Connection_Status)
// ************************************
// Function to Print MySQL Status Short
// ************************************
case "DB_Connection_Status_Report_Short":
echo $DB_Connection_Status_Short;
break; // END Function (MySQL_Connection_Status_Report_Short)
// ***********************************
// Function to Print MySQL Status Long
// ***********************************
case "DB_Connection_Status_Report_Long":
echo $DB_Connection_Status_Long;
break; // END Function (MySQL_Connection_Status_Report_Long)
// ****************************************
// Function to create a general MySQL Query
// ****************************************
case "MySQL_Query":
$MySQL_Query = $DB_Core_Command_Variable_1;
$MySQL_Commands = $MySQL_Query;
break; // END Function (MySQL_Query)
// *************************************
// Function to Verify MySQL_Query_Status
// *************************************
case "MySQL_Query_Status":
$MySQL_Connection = $DB_Core_Command_Variable_1;
if ($DB_Core_Command_Variable_2 = "Empty"){
$MySQL_Query_Success_Message = "MySQL Query Success..."; // Default if not set
} else {
$MySQL_Query_Success_Message = $DB_Core_Command_Variable_2;
}
if ($DB_Core_Command_Variable_3 = "Empty"){
$MySQL_Query_Failure_Message = "MySQL Query Failure: "; // Default if not set
} else {
$MySQL_Query_Failure_Message = $DB_Core_Command_Variable_3;
}
if (mysqli_query(${"MySQL_Connection_" . $MySQL_Connection}, $MySQL_Commands))
{
$MySQL_Query_Status_Short = 1;
$MySQL_Query_Status_Long = $MySQL_Query_Success_Message;
}
else
{
$MySQL_Query_Status_Short = 0;
$MySQL_Query_Status_Long = $MySQL_Query_Failure_Message . ": " . $MySQL_Commands . "<br>" . mysqli_error(${"MySQL_Connection_" . $MySQL_Connection});
}
break; // END Function (MySQL_Query_Status)
// ******************************************
// Function to Print MySQL Query Status Short
// ******************************************
case "MySQL_Query_Status_Report_Short":
echo $MySQL_Query_Status_Short;
break; // END Function (MySQL_Query_Status_Report_Short)
// *****************************************
// Function to Print MySQL Query Status Long
// ******************************************
case "MySQL_Query_Status_Report_Long":
echo $MySQL_Query_Status_Long;
break; // END Function (MySQL_Query_Status_Report_Long)
// *********************************
// Function to connect to MySQL
// *********************************
case "MySQL_Connect":
$MySQL_Connection = $DB_Core_Command_Variable_1;
$Database_Server_Name = $DB_Core_Command_Variable_2;
$Database_Server_Username = $DB_Core_Command_Variable_3;
$Database_Server_Username_Password = $DB_Core_Command_Variable_4;
${"MySQL_Connection_" . $MySQL_Connection} = new mysqli(
$Database_Server_Name,
$Database_Server_Username,
$Database_Server_Username_Password
);
break; // END Function (DB_Connect)
// ***************************
// If No Selection is Declared
// ***************************
default:
} // END Set Function Mode(s)
} // END - FUNCTION - < $DB_ >
?>
答案 0 :(得分:-1)
奇怪的是,我一直不得不在这个网站上回答我自己的问题,这甚至打败了这个网站,但我想出了如何创建和使用更有意义的类。希望有人会说要学习类,并且会给出一个类的例子来做到这一点。但同样,我自己想出来了。再次感谢你的帮助。很棒的网站。每个人都非常友好,乐于助人。几乎就像在美国国税局举行派对一样。
class DB {
/*
// Layout
// $DB1 -> Set_Connection_Pass_Info ("default");
// $DB1 -> Set_Connection_Fail_Info ("default");
// $DB1 -> Connection_Report_Short ();
// $DB1 -> Connection_Report_Long ();
// $DB1 -> Set_Query_Pass_Info ("default");
// $DB1 -> Set_Query_Fail_Info ("default");
// $DB1 -> Query_Report_Short ();
// $DB1 -> Query_Report_Long ();
include "class.php";
$DB1 = new DB;
// Connection Name - Server Name - User Name - User Password - Database Name: "null" if no Database
$DB1 -> Connect("Connection_1","localhost","root","UserMain1324!","Database_Test_1");
$Connection_1 -> Query (
"Connection_1",
"
"
);
$DB1 -> Close ("Connection_1");
*/
// ****************************************
// Static Public Variables
// ****************************************
static public $MySQL_Connection_ = array();
//DB::$MySQL_Connection_[$MySQL_Connection] =
static public $MySQL_Connection_Success_Message = "Connected Successfully or Still Connected";
//DB::$MySQL_Connection_Success_Message =
static public $MySQL_Connection_Failure_Message = "Connection Failed";
//DB::$MySQL_Connection_Failure_Message =
static public $MySQL_Connection_Status_Short = 0;
//DB::$MySQL_Connection_Status_Short =
static public $MySQL_Connection_Status_Long = "!!! MySQL: NOT CONNECTED !!!";
//DB::$MySQL_Connection_Status_Long =
static public $MySQL_Commands = "null";
//DB::$MySQL_Commands =
static public $MySQL_Query_Success_Message = "MySQL Query Success";
//DB::$MySQL_Query_Success_Message =
static public $MySQL_Query_Failure_Message = "MySQL Query Failure";
//DB::$MySQL_Query_Failure_Message =
static public $MySQL_Query_Status_Short = 0;
//DB::$MySQL_Query_Status_Short =
static public $MySQL_Query_Status_Long = "!!! CANT QUERY: QUERY EMPTY or NOT CONNECTED !!!";
//DB::$MySQL_Query_Status_Long =
// ****************************************
// Function to end connection to a database
// ****************************************
function Close (
$MySQL_Connection
)
{
DB::$MySQL_Connection_[$MySQL_Connection] = null;
// Set
DB::$MySQL_Connection_Status_Short = 0;
DB::$MySQL_Connection_Status_Long = "Was Connected: NOW DISCONNECTED !!!";
} // END Function (MySQL_Close)
// *****************************************************
// Function to set the $MySQL_Connection_Success_Message
// *****************************************************
function Set_Connection_Pass_Info ($Input_Variable){
switch ($Input_Variable) {
case "default":
DB::$MySQL_Connection_Success_Message = "Connected Successfully";
break;
default:
DB::$MySQL_Connection_Success_Message = $Input_Variable;
}
} // END function Set_MySQL_Connection_Success_Message
// *****************************************************
// Function to set the $MySQL_Connection_Failure_Message
// *****************************************************
function Set_Connection_Fail_Info ($Input_Variable){
switch ($Input_Variable) {
case "default":
DB::$MySQL_Connection_Failure_Message = "Connection Failed";
break;
default:
DB::$MySQL_Connection_Failure_Message = $Input_Variable;
}
} // END function $MySQL_Connection_Failure_Message
// ************************************
// Function to Print MySQL Status Short
// ************************************
function Connection_Report_Short ()
{
echo DB::$MySQL_Connection_Status_Short . "<br> <br>";
} // END Function (MySQL_Connection_Status_Report_Short)
// ***********************************
// Function to Print MySQL Status Long
// ***********************************
function Connection_Report_Long ()
{
echo DB::$MySQL_Connection_Status_Long . "<br> <br>";
} // END Function (MySQL_Connection_Status_Report_Long)
// ****************************************
// Function to create a general MySQL Query
// ****************************************
function Query (
$MySQL_Connection,
// MySQL Query
$MySQL_Query
)
{
// Create MySQL Query
// DB::$MySQL_Commands = $MySQL_Query;
// Create MySQL Query
try {
// Prepare statement
DB::$MySQL_Commands = DB::$MySQL_Connection_[$MySQL_Connection]->prepare($MySQL_Query);
// execute the query
DB::$MySQL_Commands->execute();
// Set
DB::$MySQL_Query_Status_Short = 1;
DB::$MySQL_Query_Status_Long = DB::$MySQL_Query_Success_Message;
// echo a message to say the UPDATE succeeded
// echo DB::$MySQL_Commands->rowCount() . " records UPDATED successfully";
}
// MySQL Query Error Handling
catch (PDOException $PDOException_Error)
{
// Set
DB::$MySQL_Query_Status_Short = 0;
DB::$MySQL_Query_Status_Long = DB::$MySQL_Query_Failure_Message . ": " . $MySQL_Query . "<br>" . $PDOException_Error->getMessage();
}
} // END Function (MySQL_Query)
// *************************************************
// Function to set the $MySQL_Query_Success_Message;
// *************************************************
function Set_Query_Pass_Info ($Input_Variable){
switch ($Input_Variable) {
case "default":
DB::$MySQL_Query_Success_Message = "MySQL Query Success";
break;
default:
DB::$MySQL_Query_Success_Message = $Input_Variable;
}
} // END function Set_MySQL_Connection_Success_Message
// *************************************************
// Function to set the $MySQL_Query_Failure_Message;
// *************************************************
function Set_Query_Fail_Info ($Input_Variable){
switch ($Input_Variable) {
case "default":
DB::$MySQL_Query_Failure_Message = "MySQL Query Failure";
break;
default:
DB::$MySQL_Query_Failure_Message = $Input_Variable;
}
} // END function $MySQL_Connection_Failure_Message
// ******************************************
// Function to Print MySQL Query Status Short
// ******************************************
function Query_Report_Short ()
{
echo DB::$MySQL_Query_Status_Short . "<br> <br>";
} // END Function (MySQL_Query_Status_Report_Long)
// *****************************************
// Function to Print MySQL Query Status Long
// *****************************************
function Query_Report_Long ()
{
echo DB::$MySQL_Query_Status_Long . "<br> <br>";
} // END Function (MySQL_Query_Status_Report_Long)
// Main function //
// ****************************
// Function to connect to MySQL
// ****************************
function Connect(
$MySQL_Connection,
$Database_Server_Name,
$Database_Server_Username,
$Database_Server_Username_Password,
$Database_Name // Set to "null" if not connecting to a database
)
{
// Create Connection
try {
// Add database portion of cononect statement if connecting to a database
if($Database_Name = "null"){
$Database_Connection_Addon = "";
} else {
$Database_Connection_Addon = ";dbname=$Database_Name";
}
// Create Connection
DB::$MySQL_Connection_[$MySQL_Connection] = new PDO("mysql:host=$Database_Server_Name" . "$Database_Connection_Addon", $Database_Server_Username, $Database_Server_Username_Password);
// Connection Attributes
DB::$MySQL_Connection_[$MySQL_Connection]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Set
DB::$MySQL_Connection_Status_Short = 1;
DB::$MySQL_Connection_Status_Long = DB::$MySQL_Connection_Success_Message;
}
// Connection Error Handling
catch (PDOException $PDOException_Error)
{
// Set
DB::$MySQL_Connection_Status_Short = 0;
DB::$MySQL_Connection_Status_Long = DB::$MySQL_Connection_Failure_Message . ": " . $PDOException_Error->getMessage();
}
} // END Function (MySQL_Connect)
} // END CLASS (DB)