<?php
ini_set('display_errors', 'On');
error_reporting(E_ERROR);


/*DEPRECATED Use version 2*/
/* The following code searches for names in the PROFILE table and returns the name and email of any matches*/

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['name'])){
	$email = $_POST['name'];
	
	// include db config class
	require_once __DIR__ . '/db_config.php';
	
	// connecting to db
    	$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
    	mysqli_set_charset($mysqli, "utf8");
    	
	if($stmt = $mysqli->prepare("SELECT DISTINCT NAME FROM PROFILE WHERE USER_ID = (SELECT USER_ID FROM USER_ID WHERE EMAIL LIKE ?)")){
		$stmt->bind_param("s", $email);
		$stmt->execute();
		$stmt->bind_result($searched_name);
		while ($stmt->fetch()) {    		
	    		$arr_name[] = $searched_name;			
		}
				
		//put email in an array
		$arr_email[] = $email;
				
		// if values are available for email or names then echo JSON response
		if((isset($arr_email) && !is_null($arr_email)) || (isset($arr_name) && !is_null($arr_name))){
			echo json_encode(array('arr_email'=>$arr_email, 'arr_name'=>$arr_name));			
		}
		else{			
			$arr_email[] = "no records found";
			echo json_encode(array('arr_email'=>$arr_email));
		}
				
		// Close the prepared statement.
	    	$stmt->close(); 
	}				 		 	    	
	else{
    		$response["success"] = 0;
		$response["message"] = "Failed to fetch data";
				 
		// echoing JSON response
		echo json_encode($response);
    	}  
	
	mysqli_close($mysqli);
}
else{
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
}

?>