Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class loginManager
- {
- /*
- * Class loginManager functions:
- * __construct(): Constructor. Pass the parameters of the MySQL connection variables and it will create the object and then calls mysqlConnect().
- * mysqlConnect(): Creates an OO connection to MySQL using the parameters passed.
- * mysqlQuery(): Sanitizes the SQL query by mysqlSanitize() and then runs it from the connection created using mysqlConnect().
- * * Returns an 3D array where the first bracket is the row number. The second bracket is the column name.
- * mysqlSanitize(): Sanitizes the SQL Query passed as a parameter by using mysqli_real_escape_string().
- * returnMax(): Returns the greatest value of the parameters passed into the functions.
- */
- /*
- * Variables beginning with $database involve the database username, password, or host.
- */
- private $database_host = "";
- private $database_user = "";
- private $databate_pass = "";
- private $database_name = "";
- /*
- * Variables beginning with $sql involve SQL queries or connections.
- */
- private $sql_conn;
- function __construct()
- {
- //This constructor needs to be passed four parameters, the host, user, pass, and name of the db (in that order).
- switch (func_num_args())
- {
- case 4:
- //If the number of parameters passed is correct, put them into an array.
- $argarray = func_get_args();
- /*
- * (The above array)
- * $argarray[0] = database host
- * $argarray[1] = database username
- * $argarray[2] = database password
- * $argarray[3] = database name
- */
- //Next, set the variables that were passed as parameters.
- $this->setHost($argarray[0]);
- $this->setUser($argarray[1]);
- $this->setPass($argarray[2]);
- $this->setName($argarray[3]);
- //Next, connect to the MySQL database with the previous information.
- $this->mysqlConnect($this->database_host, $this->database_user, $this->database_pass, $this->database_name);
- break;
- default:
- //If there were not four parameters passed into the constructor, end the creation of it.
- die("There were not four parameters passed into the constructor of the loginManager class. There were only " . func_num_args() . " passed.");
- break;
- }
- }
- private function mysqlConnect($database_host, $database_user, $database_pass, $database_name)
- {
- //This function connects to a MySQL database.
- $this->sql_conn = new mysqli($database_host, $database_user, $database_pass, $database_name);
- if (mysqli_connect_error())
- {
- //If there was an error connecting, call the die function and close the connection.
- $this->sql_conn->close();
- die("mysqlConnect could not be completed: " . mysqli_connect_error());
- }
- }
- public function mysqlSanitize($string)
- {
- //This function will sanitize a string that was passed as a parameter and return it.
- return mysqli_real_escape_string($string);
- }
- public function mysqlQuery($query)
- {
- //Check and make sure the the $sql_conn variable is set.
- if (!isset($this->sql_conn))
- {
- //If the $sql_conn variable is not set, call the die function.
- die("You have not created a connection to a MySQL database.");
- }
- if (!is_string($query))
- {
- //If a string was not passed as a parameter, call the die function.
- die("An SQL query must be passed as a paramter int he mysqlQuery function.");
- }
- else
- {
- //If a string was passed as a parameter, then run the query.
- $resultarray = array();
- $result = $this->sql_conn->query($query);
- while ($row = $result->fetch_array())
- {
- //As long as there are rows that matched the query, add to the array.
- $resultarray[] = $row;
- }
- //Now, close the results and return the values.
- $result->close();
- return $resultarray;
- /*
- * $resultarray[x][y]:
- * [x] = the row number.
- * [y] = the column name.
- */
- }
- }
- public function returnMax()
- {
- //Create an array of the parameters passed.
- $argarray = func_get_args();
- //Create a blank variable that will later contain the largest variable.
- $largest = 0;
- //Loop through all of the parameters passed.
- foreach ($argarray as $value) {
- //Check if the value is an integer.
- if (!is_numeric($value))
- {
- //If the current value is not a number, skip the value.
- continue;
- }
- if (intval($value) > intval($largest))
- {
- //If the new value is larger than the previous largest one, update it!
- $largest = $value;
- }
- }
- }
- function setHost($var)
- {
- $this->database_host = $var;
- }
- function setUser($var)
- {
- $this->database_user = $var;
- }
- function setPass($var)
- {
- $this->database_pass = $var;
- }
- function setName($var)
- {
- $this->database_name = $var;
- }
- }
- $loginManager = new loginManager("127.0.0.1","root","", "logintest");
- $resultarray = $loginManager->mysqlQuery("SELECT * FROM users");
- echo $loginManager->returnMax(4,6,7,2);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement