Reputation:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'blood_db';
$connection = new mysqli($dbhost,$dbuser,$dbpass,$db);
if($connection->connect_error){
die("Server Error ".$connection->connect_error);
}
else{
$query = ("SELECT first_name FROM accounts WHERE email_address = '".$_SESSION['username']."'");
$obj = $connection->query($query);
echo "Welcome ".$obj->first_name;
here it shows a notice, which is " Notice: Undefined property: mysqli_result::$first_name " It is returning the object, So how would i extract the first name from it?
// printf("Welcome %s",$result->);
echo '<br><a href="logout.php">Logout</a>';
Upvotes: 2
Views: 88
Reputation: 392
A very basic example would be like this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$firstname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM tbl_test WHERE email_address = '". $_SESSION['username']. "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Retrieve Firstname
while($row = $result->fetch_assoc()) {
$firstname = $row["first_name"];
}
} else {
echo "No results found!";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<header>
<h3>Welcome <?php echo $firstname; ?></h3>
</header>
</body>
</html>
<?php
$conn->close();
?>
Upvotes: 0
Reputation: 5690
you can try this which select data and add condition if you want to check that data empty or not
$obj = $connection->query($query);
if ($obj->num_rows > 0) {
$row = $obj->fetch_assoc();
echo "Welcome ".$row['first_name'];
}
if you do not want to check data have then use this
$row = $obj->fetch_assoc();
echo "Welcome ".$row['first_name'];
for more information
https://www.w3schools.com/php/php_mysql_select.asp
Upvotes: 1
Reputation: 1829
use below code
$result = $connection->query($query);
if($result){
$row = $result->fetch_assoc();
echo "Welcome ".$row['first_name'];
}else{
// check error
}
Upvotes: 1