<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
/*
* Use this instead of $connect_error if you need to ensure
* compatibility with PHP versions prior to 5.2.9 and 5.3.0.
*/
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
function select_data_func($sqlSelect){
global $mysqli;
conn_db_func();
$result = mysqli_query($mysqli, $sqlSelect) or die(mysqli_error());
close_db_func();
return $result;
}
function manage_data_func($sql){
global $mysqli;
conn_db_func();
$result = mysqli_query($mysqli, $sql) or die(mysqli_error());
$affect = mysqli_affected_rows($mysqli);
close_db_func();
return $affect;
}
function insert_data_func($sqlSelect, $sqlInsert){
global $mysqli;
conn_db_func();
$result_se = mysqli_query($mysqli, $sqlSelect) or die(mysqli_error());
$num_row = mysqli_num_rows($result_se);
if($num_row == 0){
$result_inst = mysqli_query($mysqli, $sqlInsert) or die(mysqli_error());
$affect = mysqli_affected_rows($mysqli);
}
close_db_func();
return $affect;
}
function insert_returnid_func($sqlInsert, $sqlSelect){
global $mysqli;
conn_db_func();
$result = mysqli_query($mysqli, $sqlInsert) or die(mysqli_error());
$affect = mysqli_affected_rows($mysqli);
if($affect > 0){
$result = mysqli_query($mysqli, $sqlSelect) or die(mysqli_error());
close_db_func();
return $result;
}
}
function delete_data_func($ID, $table, $field){
global $mysqli;
conn_db_func();
$sqlSelect = "SELECT * FROM $table WHERE $field = '$ID' ";
$result = mysqli_query($mysqli, $sqlSelect) or die(mysqli_error());
$num_rows = mysqli_num_rows($result);
if($num_rows == 0){
close_db_func();
return true;
}elseif($num_rows > 0 ){
$sqlDelete = "DELETE FROM $table WHERE $field = '$ID' ";
$result = mysqli_query($mysqli, $sqlDelete) or die(mysqli_error());
$affect = mysqli_affected_rows($mysqli);
close_db_func();
if($affect > 0){ return true; }else{ return false; }
}else{
return false;
}
}