LampPi

 

Code


MySQL: Database Connections


...
$lp = new lamppi_class();
...

...
# Set database connection
$mysqli_link = $lp->openDBConnection($MYSQL_HOST, $MYSQL_USER, $MYSQL_PW, $MYSQL_DB);
# Perform some actions
...
# Close DB connection
$mysqli_link = $lp->closeDBConnection($mysqli_link);
...

...
public function OpenDBConnection($mysql_host, $mysql_user, $mysql_pw, $mysql_db) {
# Initialization
$mysqli_link = "";

# Connect
$mysqli_link = new mysqli($mysql_host, $mysql_user, $mysql_pw, $mysql_db);
if ($mysqli_link->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli_link->connect_errno . ") " .
$mysqli_link->connect_error;
}

# Return connection string
return $mysqli_link;
}

public function closeDBConnection($mysqli_link) {
# Initialization
$thread_id = "";

# Get the thread id
$thread_id = $mysqli_link->thread_id;
if ($mysqli_link->error) {
printf("Error: %s\n", $mysqli_link->error);
}

# Kill the connection thread
$mysqli_link->kill($thread_id);

# Close the connection
$mysqli_link->close();

# Clear $mysqli_link
unset($mysqli_link);
$mysqli_link = "";

# Return null connection string
return $mysqli_link;
}


About

Contact

Links

Site Map

© LampPi 2018