I have database structure like this:
table name: tblpost and I want to show all post by user like show post by multiple user in only page name: allpost.php
And I want to get result like this:
So how to show data like that? Many thanks for all your answer.
Assuming you use PDO.
$stmt = $db->prepare("SELECT * from tblposts where post_by=:post_by");
$stmt->bindParam(':post_by',$user);
$stmt->execute();
//Fetches the posts for the certain user you want
$posts = $stmt->fetch(PDO::FETCH_ASSOC);
MySQLi:
$stmt = $mysqli->prepare("SELECT * from tblposts where post_by=?");
$stmt->bind_param('s',$user);
$stmt->execute();
$res = $stmt->get_result();
while ($row = mysqli_fetch_assoc($res)) {
echo $row['post_date'];
}
Related
Frist I have table of general notifications this table have three field ID+Body+Date and from my app flutter any message send to users save this message in this table.
Now the problem is, the message goes to all users, so I'm trying to make a way to find out which users have seen this message from others.
Now I make another table to save userID and notificationID.So if Userid and NotificationsID in IsWatchedNotifications table this means message has been viewed and if not have data then this means message not viewed to now.
Now I have problem with that query how I can write it to work like that?
this is my query line to now:
$sql = "SELECT * FROM Notifications
LEFT JOIN IsWatchedNotifications ON Notifications.NotificationsID = IsWatchedNotifications.NotificationsID
where ";
Full code:
<?php
$UserID= $_GET['UserID'];
$sql = "SELECT * FROM Notifications
LEFT JOIN IsWatchedNotifications ON Notifications.NotificationsID = IsWatchedNotifications.NotificationsID
where ";
$stmt = $con->prepare($sql);
$stmt->bind_param("s",$UserID);
$stmt->execute();
$result = $stmt->get_result();
if ($result) {
while($row[] = $result->fetch_assoc()) {
$item = $row;
$json = json_encode($item, JSON_NUMERIC_CHECK);
}
} else {
}
echo $json;
$con->close();
?>
Anyone can help me
Thank you
assumeing that your column name is user_id
You need only to add the condition with placeholder
$sql = "SELECT * FROM Notifications
LEFT JOIN IsWatchedNotifications ON Notifications.NotificationsID = IsWatchedNotifications.NotificationsID
where user_id = ?";
Users can add their workouts into a database. There will have more than one workout per user. How can i display the workout names for the logged in user, underneath each other? I have no problem adding the workouts. When I echo the names of the added workouts, it display right next to each other like "workout1workout2". I want to be able to display the workouts underneath each other. I have no idea how to do it. It would be best if I could display each workout as a button.
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
echo $data['workout'];
}
Firstly, you shouldn't put that username in query like that. Look into SQL Injection and how to avoid that in PHP.
As for displaying buttons/workouts. You can embed HTML code inside the echo statement like below. You can add any html like that and it will render HTML.
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
echo "<button>$data['workout']</button></br>";
}
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' Group By username";
I am trying to count a specific field in a database that has user details in it. Every user has an id and I want to count the amount of users/ids that are registered in the database. This is my code, how can I solve it? Because it does not echo anything. I am not allowed to use MYSQL to retrieve the count.
include ("databaseconnectie.php");
$query = $db->prepare("
SELECT
COUNT(id) as total
FROM
users");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
echo $result['total'];`
Due to the way you fetch'ed your data, you will need to add [0] in the $result echo. ($result[0]['total']).
I added in a print_r($result) to show me the array so I could identify where the issue came from and how I can step through the array to get my intended result.
$query = $db->prepare("SELECT COUNT(id) as total FROM users");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
print_r($result);
echo $result[0]['total'];
EDIT:
As mentioned by #Federkun; you could change this line:
$result = $query->fetchAll(PDO::FETCH_ASSOC);
to:
$result = $query->fetch(PDO::FETCH_ASSOC);
Then you wouldn't need to add the [0] as it is just fetching one result.
another question. I need to display a username and so what I'm doing is getting the author id (which is '1'), and then using a query saying get the username from the users table where the author id is the same as the user id.
My problem is that I'm getting the value of '1' returned as I've said above and the following is my code. Am I missing something here or...?
$users = mysqli_query($sql, "SELECT * FROM users WHERE userid = '$authorid'") or die($users . "<br/>" . mysqli_error($sql));
while($userData = mysqli_fetch_array($users)) {
$postAuthor = $usersData['username'];
}
Edit 1
I said also mention the above information should show the name 'Dan'.
The user 'Dan' has a user id of 1 should the author id should be 1 (which it is) from the post row. This is the only use of anything to do with the user table so it's not being overridden anywhere. I'm so confused.
This should have been an error:
while($userData = mysqli_fetch_array($users)) {
^^ no s
$postAuthor = $usersData['username'];
^^ used different variable name
}
Note: Use prepared statements also in this case, you're using mysqli anyways.
$select = $sql->prepare("SELECT * FROM users WHERE userid = ?");
$select->bind_param('i', $author_id);
$select->execute();
$results = $select->get_result();
while($userData = $results->fetch_assoc()) {
$postAuthor = $userData['username'];
}
I have a drop down menu populated from a mysql database , is it possible to display two columns from the database but only when clicked ?
By this I mean, when you click you see a list of items on the left and a brief description on the right. The description column should only be visible when clicking on the dropdown.
Is this possible ?
My code is as follow, this queries the DB to check the current selection for the user.
I have tried adding a second column but it shows at all times which looks terrible.
<?php
require_once('db_connect.php');
$id = $_GET['id'];
$qry = "SELECT issue FROM issuetbl WHERE issueid =?";
$stmt = $mysqli->prepare($qry);
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
$client_result = $result->fetch_assoc();
$qry2 = "SELECT data FROM issdd";
$stmt = $mysqli->prepare($qry2);
$stmt->execute();
$resultdata = $stmt->get_result();
echo '<select name="enqiss">';
while($row = $resultdata->fetch_assoc()) {
if($client_result['issue']==$row['data']){
$selectCurrent='selected';
}else{
$selectCurrent='';
}
echo '<option value="'.$row['data'].'" '.$selectCurrent.'>'.$row['data'].'</option>';
}
echo '</select>';
?>
Many thanks