I want to know if there is a way to show/display data from database in html without using session..For examle i have a function SelectMember
that will select a member from member table then i want to display the details of this member in html as echo. Is there a way to do this without using session?I was able to do it with session but as much as possible i dont want to use session in this time..The function i am talking about is from a separate page that i include in the index of html.In the html is where i want to display the detail. Any suggestion is appreciated
i tried calling the function in the html then i put the variable in echo but the variable in undefined
question is how to fix the undefined variable problem without using session
update
function EventMember(){
global $dbh;
if(!empty($_POST['membername'])){
$membername = trim($_POST['membername']);
$stmt = $dbh->prepare("SELECT * FROM member WHERE mem_name = ?") ;
$stmt->bindValue(1,$membername);
$stmt->execute();
$selected_row = $stmt->fetch(PDO::FETCH_ASSOC);
if($selected_row){
$_SESSION['id'] = $selected_row['mem_id'];
$_SESSION['name'] = $selected_row['mem_name'];
$_SESSION['age'] = $selected_row['mem_age'];
$_SESSION['address'] = $selected_row['mem_address'];
$_SESSION['sex'] = $selected_row['mem_sex'];
//$memid = $selected_row['mem_id'];
//$memname = $selected_row['mem_name'];
//$memage = $selected_row['mem_age'];
//$memaddress = $selected_row['mem_address'];
//$memsex = $selected_row['mem_sex'];
header("Location: searchresult.php");
exit;
}else{
echo "No member found";
}
}else{
echo "name is empty";
}
}
<tr>
<td valign="top">
<label for="name">name</label>
</td>
<td valign="top">
<input type="text" name="name" maxlength="50" size="30" value="<?php echo $memname;?>">
</td>
if i uncomment the $memname
in database side and use it it will be undefined but if i use $_SESSION['name']
it will show result so how to fix this
Another Update: So you want to send the data to another page without using sessions...
The easiest way to do this would be to change your header() statement to contains get parameters. But you cannot do this if the data is sensitive...
header("Location: searchresult.php?name=".$selected_row['mem_name']);
And then on the second page, just use echo isset($_GET['name']) ? $_GET['name'] : "";
- make sure to sanitize the data though.
Other than that, you're going to get more complicated with how you're trying to do this - if the above does not work for you, I'd highly recommend you approach this differently.
...
Update: Now that code is provided, I'm adding this to my original answer...
You get an undefined error because you are outside of the scope of the variable you're trying to access. You need to move the html up into the if statement. I did this for you but you should format it better with the echo statement.
Also, you previously had a header() command in that if statement. This will NOT work if you don't use session or something else so that the page you redirect to can access the variable's value.
if ($selected_row){
echo '<tr>
<td valign="top">
<label for="name">name</label>
</td>
<td valign="top">
<input type="text" name="name" maxlength="50" size="30" value="'.$selected_row['mem_name'].'">
</td>';
} else {
echo "No member found";
}
} else {
echo "name is empty";
}
}
...
Old Answer:
You retrieve the data without sessions, unless you store the data in session and call to that.
You don't need to access it via session, though it's better to store it somewhere (like...session) instead of querying the database repeatedly.
There is no need to use session for retrieval of the data. Session is only to store data for the application level access.