PHP
Database/MySQL
SELECT
$conn = new mysqli("<database_host>", "<database_user>", "<database_password>", "<database_name>");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM <table> WHERE <condition>;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
// Do your Stuff (acces values with $row["<row_name>"])
}
}
$conn->close();
INSERT
$conn = new mysqli("<database_host>", "<database_user>", "<database_password>", "<database_name>");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Simple Query
$query = "INSERT INTO <table> (<row_name_1>, <row_name_2>) VALUES ('<value_row_1>', '<value_row_2>')";
// Prevent SQL-Injection
$query = sprintf("SELECT CountryCode FROM City WHERE name='%s'",
$mysqli->real_escape_string($city));
if ($conn->query($query) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . $conn->error;
}
$conn->close();
REMOVE
$conn = new mysqli("<database_host>", "<database_user>", "<database_password>", "<database_name>");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM <table> WHERE <condition>;";
if ($conn->query($sql) === TRUE) {
$ret_obj->message = "Entrys deleted";
} else {
$ret_obj->message = "Error deleting record: " . $conn->error;
}
$conn->close();
Error Reporting
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
Create Objects (for JSON-API)
$ret_array = [];
//...
while($row = $result->fetch_assoc()) {
$tempObj = new stdClass();
$tempObj->value_1 = "Hello World";
$tempObj->value_2 = "TEST_1234";
$tempObj->count = $row["COUNT(*)"];
array_push($ret_array, $tempObj);
}
//...
return $ret_array;
XML Interacting
$xml_content = simplexml_load_string($xml_string)
print($xml_content->getName()."<br>");
foreach ($xml_content->children() as $child){
print("::". $child->getName() ."<br>");
foreach ($child->children() as $grand_child){
print(":::::". $grand_child->getName() ."<br>");
}
}
Get Content of First Child
print(simplexml_load_string($resp)->children()[0][0]);
cURL (Web-Requests)
$url = <url_string>;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($curl);
curl_close($curl);
API-Example
<?php
if(gethostname() == 'dev-server') {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$dev = true;
} else {
$dev = false;
}
try {
$messageObject = new stdClass();
$servername = "<servername>";
$username = "<username>";
$password = "<password>";
$dbname = "<db_name>";
$conn = new mysqli($servername, $username, $password, $dbname);
if (isset($_GET["function"])) {
$function = $_GET["function"];
} else {
$function = "";
}
if (isset($_GET["<param_2>"])) {
$param_2 = $_GET["<param_2>"];
} else {
$param_2 = "";
}
if ($function == "<function_1>") {
$sql = "SELECT * FROM <table>";
$result = $conn->query($sql);
$events_array;
$events_array['<event_type>'] = [];
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$event = new stdClass();
$event->value_1 = $row["<value_1>"];
$event->value_2 = $row["<value_2>"];
array_push($events_array['<event_type>'], $event);
}
}
$messageObject->message = $events_array;
$messageObject->code = 200;
} else {
$messageObject->message = "Please specifi which function you want to call.";
$messageObject->code = 200;
}
} catch (exception $e) {
$messageObject->message = "Something went wrong!";
$messageObject->code = 500;
$messageObject->error = "Message: ".$e;
} finally {
$conn->close();
header('Content-Type: application/json');
echo json_encode($messageObject);
}
?>
CSV export for Excel
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$text = "";
// To ignore SSL Verification
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$url = "get_your_content";
$content = file_get_contents($url, false, stream_context_create($arrContextOptions));
$content = json_decode($content, true);
for ($x = 0; $x < sizeof($content['message']['...']); $x++) {
// do your stuff here
}
$output = fopen("php://output", "w");
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="yourFile.csv"');
fputs($output, $bom = (chr(0xEF) . chr(0xBB) . chr(0xBF)));
fputs($output, $text);
?>