I have made a function in PHP to grab info from a MySQL database but am not sure about something.
Currently the function looks like this:
function profile_info($option, $size = NULL){
// MySQL Connection Info
$mysql_hostname = "";
$mysql_username = "";
$mysql_password = "";
$mysql_database = "";
$mysql_table = "";
// MySQL Connect
$con = mysqli_connect($mysql_hostname,$mysql_username,$mysql_password,$mysql_database);
// Check the Database Connection
if (mysqli_connect_errno()){
echo (mysqli_connect_error());
}
// Define UID
$uid = $_SESSION['login'];
// Deploy Query
$result = $con->query("SELECT * FROM $mysql_table WHERE uid='$uid'");
// Define Row For All Data
$row = $result->fetch_assoc();
if($option == "firstname"){
echo $row['first_name'];
}
if($option == "lastname"){
echo $row['last_name'];
}
if($option == "nickname"){
echo $row['nick_name'];
}
if($option == "email"){
echo $row['email'];
}
if($option == "dob"){
echo $row['date_of_birth'];
}
if($option == "status"){
echo $row['status'];
}
if($option == "gravitar"){
echo ("http://www.gravatar.com/avatar/" . md5( strtolower( trim( $row['email'] ) ) ) . "?d=mm&s=" . $size);
}
$result->close();
$con->close();
}
I've tested it and it works perfectly.
Now my question is, does it make a new connection to the database everytime I call profile_info?
If so, how do I fix it so that it only calls the database once for all the information.
Regards, Tim
mysql_functions is bad practice and then goes on to write broken exploitable code withmysqli.