PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE staff (surname varchar(20), birthday datetime); INSERT INTO staff (surname, birthday) VALUES ('Smith', '1990-01-01'), ('Jones', '1980-12-12'), ('Johnson', '2005-05-31'); SELECT * FROM staff;
Copy Clear
Copy Format Clear
<?php session_start(); //add some dummy session data for the demo: $_SESSION = array("loggedin"=> true, "name"=>"Piet", "id_staff"=> 9); var_dump($_SESSION); // Run query using mysqli /*$result = $mysqli->query($query); $version = $result->fetch_object(); printf('DB version (mysqli): %s ' . PHP_EOL, $version->version);*/ class Core { public static $link; public static function getStaffList(){ if ($_SESSION['name'] =="admin" || $_SESSION['name'] =="Piet") { $sql = "SELECT * FROM staff"; } else { $sql = "SELECT * FROM staff where surname = '".$_SESSION['name']."'"; } echo "Executing the following SQL:".$sql.PHP_EOL; $result = Core::$link->query($sql); if (!$result) { return 'Error: ' . mysqli_error(Core::$link); } $return = array(); while ($myrow = mysqli_fetch_assoc($result)) { if ($myrow['birthday'] !== '0000-00-00') { $myrow['birthday'] = date("d.m.Y", strtotime($myrow['birthday'])); } else { $myrow['birthday'] = ''; } $return[] = $myrow; } return $return; } } //some assignment, to make the demo database work: Core::$link = $mysqli; $data = Core::getStaffList(); var_dump($data);
Show:  
Copy Clear