I am trying to fetch data from the database and getting the error as:
Warning: Attempt to read property “username” on null in C:UsershpDesktopxampphtdocs19Aprilmain.php on line 33
Please suggest/guide me through the solution
My main.php file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h3>Search Meetings</h3>
<form action="" method="POST">
<!-- <label>Enter Your Name:</label><br /> -->
<input type="text" name="username" placeholder="Your Name" required/>
<br /><br />
<button type="submit" name="submit">Search</button>
</form>
</body>
</html>
<?php
if(isset($_POST['username']) && $_POST['username']!=""){
$username = $_POST['username'];
$url = "http://localhost/19April/api/".$username;
$client = curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($client);
$result = json_decode($response);
// echo "<table>";
echo "<tr><td>User Name:</td><td>$result->username</td></tr>";
// echo $result->$username;
// echo "<tr><td>Age:</td><td>$result->age</td></tr>";
// echo "<tr><td>Email-Id:</td><td>$result->email</td></tr>";
// echo "</table>";
}
?>
My api.php file
<?php
header("Content-Type:application/json");
if (isset($_GET['username']) && $_GET['username']!="") {
include('db.php');
$username = $_GET['username'];
$result = mysqli_query($con,"SELECT * FROM `transactions` WHERE username=$username");
if(mysqli_num_rows($result)>0){
$row = mysqli_fetch_array($result);
$username = $row['username'];
$age = $row['age'];
$email = $row['email'];
response($username, $username, $age,$email);
mysqli_close($con);
}else{
response(NULL, NULL, 200,"No Record Found");
}
}else{
response(NULL, NULL, 400,"Invalid Request");
}
function response($username,$age,$email){
$response['username'] = $username;
$response['age'] = $age;
$response['email'] = $email;
$json_response = json_encode($response);
echo $json_response;
}
?>