Хорошо, вот функция, над которой я работаю. Теперь учтите, что я новичок в программировании, и в этом, вероятно, больше неправильного, чем то, что я спрашиваю. Как установить переменную:
${"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_ >
?>