ARTICLE AD BOX
Do I have shorter options?
Im trying to understand this code, but I got lost midway...
Im trying to connect a database, where I can write small codes, that I'll use later with JavaScript and HTML.
This is the way my teacher thaught it, but im scared I dont understand it 100%.
The part I dont really understand:
<?php function dataread($m, $type = null, $data= null){ $db= new mysqli('localhost', 'root', '', 'databasename'); if ($db->connect_errno != 0) { return $db->connect_errno; } if(!is_null($type) && !is_null($data)){ $stmt = $db->prepare($m); $stmt -> bind_param($$type, ...$data); $stmt -> execute(); $result=$stmt->get_result(); } else{ $result= $db->query($m); } if ($db->errno != 0) { return $db->error; } if ($result->num_rows == 0) { return []; } return $result->fetch_all(MYSQLI_ASSOC); } function datachange($m, $type = null, $data= null){ $db= new mysqli('localhost', 'root', '', 'databasename'); if(!is_null($type) && !is_null($data)){ $stmt = $db->prepare($m); $stmt -> bind_param($type, ...$data); $stmt -> execute(); } else{ $db->query($m); } if ($db->connect_errno != 0) { return $db->connect_errno; } if ($db->errno != 0) { return $db->error; } return $db->affected_rows > 0 ? "Done" : "Missing"; } ?>And for another php code I use this, this is clear.
include "./index1.php"; $method = $_SERVER["REQUEST_METHOD"]; $uri = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); $uri = explode("/", $uri); $bodydata = json_decode(file_get_contents("php://input"), true);I just write this for an example
$aaaSQL="SELECT `this` FROM `that` WHERE `this`= ?;"; $aaa=dataread($aaaSQL, "s", [$bodydata["this"]]);Thank you for helping! :")
