Uncategorized

Sample Script For Select Statement PHP MYSQL API

Here is a sample script for select statement using bind param for PHP MYSQL API

<?php

// Enable CORS headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST");
header("Access-Control-Allow-Headers: Content-Type");


require_once 'db.php';

// Retrieve the username and password from the POST request
$username = $_POST['username'];
$password = $_POST['password'];

// Encrypt the password using MD5
$encryptedPassword = md5($password);

// Query the database to check if credentials match
$query = "SELECT * FROM users WHERE user_phone = :username AND user_password = :password AND status = 'Active'";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $encryptedPassword);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if ($user) {
    // Valid credentials
    $response = [
        'success' => true,
        'message' => 'Login successful',
        'user_phone' => $user['user_phone'],
        'user_name' => $user['full_name'],
      	'user_email' => $user['user_email'],
      	'user_grade' => $user['user_grade'],
      'user_school' => $user['school'],
        'id' => $user['id'],
    ];
} else {
    // Invalid credentials
    $response = [
        'success' => false,
        'message' => 'Invalid username or password',
    ];
}

// Send the response as JSON
header('Content-Type: application/json');
echo json_encode($response);

Leave a Reply

Your email address will not be published. Required fields are marked *