Skip to content

Instantly share code, notes, and snippets.

@serdoune
Last active February 4, 2018 10:56
Show Gist options
  • Save serdoune/82b2d86be82cc908a71c7c7cfd1fb7ac to your computer and use it in GitHub Desktop.
Save serdoune/82b2d86be82cc908a71c7c7cfd1fb7ac to your computer and use it in GitHub Desktop.
<?php
define('hostname', 'localhost');
define('user', 'root');
define('password', '');
define('databaseName', 'tutorial');
$connect = mysqli_connect(hostname, user, password, databaseName);
?>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
require 'connection.php';
createStudent();
}
function createStudent()
{
global $connect;
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$age = $_POST["age"];
$query = " Insert into student(firstname,lastname,age) values ('$firstname','$lastname','$age');";
mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);
}
?>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
include 'connection.php';
showStudent();
}
function showStudent()
{
global $connect;
$query = " Select * FROM STUDENT; ";
$result = mysqli_query($connect, $query);
$number_of_rows = mysqli_num_rows($result);
$temp_array = array();
if($number_of_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$temp_array[] = $row;
}
}
header('Content-Type: application/json');
echo json_encode(array("students"=>$temp_array));
mysqli_close($connect);
}
?>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
require 'connection.php';
createStudent();
}
function createStudent()
{
global $connect;
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$age = $_POST["age"];
$query = " Insert into student(firstname,lastname,age) values ('$firstname','$lastname','$age');";
mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);
}
?>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
include 'connection.php';
showStudent();
}
function showStudent()
{
global $connect;
$query = " Select * FROM STUDENT; ";
$result = mysqli_query($connect, $query);
$number_of_rows = mysqli_num_rows($result);
$temp_array = array();
if($number_of_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$temp_array[] = $row;
}
}
header('Content-Type: application/json');
echo json_encode(array("students"=>$temp_array));
mysqli_close($connect);
}
?>
<?php
define('hostname', 'localhost');
define('user', 'root');
define('password', '');
define('databaseName', 'tutorial');
$connect = mysqli_connect(hostname, user, password, databaseName);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment