Get data from different table and column - php

I am making a basic "blog" where different people with different user avatars are able to post content. I have 3 tables in MySQL, users, posts and following.
What I am trying to do is to display the avatar of the user who posted the post. And I can't get this to work, I have the code I've been working on below. It's little bit longer but it's mostly echo results in the end, I didn't include these in my example since the code got big. What is the correct way to get whoever posted what's avatar since it's in two different tables? I tried messing around with mysqli_multi_query earlier with no luck.
include "connect.php";
$user_id = $_SESSION["user_id"];
$query = "SELECT * FROM posts
WHERE user_id = $user_id OR (user_id IN (SELECT user2_id FROM following WHERE user1_id='$user_id'))
AND (SELECT user_img FROM users where user_img='$user_img')
ORDER BY timestamp DESC";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query);
while($row = mysqli_fetch_array($result)){
$postid = $row['id'];
$post = $row['post'];
$username = $row['username'];
$timestamp = $row['timestamp'];
$user_img = $row['user_img'];
$type = -1;
}

Related

Do I need a left, natural or simple join in SQL?

I am new to PHP coding and just trying to fix some functionality on my site that was left over from the lead developer.
The site, [Vloggi], is a marketplace. So I need to show the name of the job poster in the assignments page . The table I have the jobs in only has the ID, not the name.
So I need a join, but I've tried and it breaks the entire site.
The SQL has 17 tables, I need to display the User Name (usr_name) contained in table 3, the organisation contained in table 7 (usrg_orgname) with the job posting user (vlop_usr_id) details in table 14.
The primary key is users.usr_id, which is linked to users_gor.usrg_usr_id and vlog-ops.vlog_usr_id.
Table 3: users
usr_id, usr_email, usr_password, usr_fbuser, usr_fbtoken, usr_name, usr_loc_name, usr_loc_lat1, usr_loc_lon1, usr_loc_lat2, usr_loc_lon2, usr_status, usr_gor, usr_vgr, usr_token, usr_regtoken,
table 7: users_gor
usrg_usr_id, usrg_creditops, usrg_creditvlog, usrg_creditvlogette, usrg_destination, usrg_orgname, usrg_orgtype, usrg_location, usrg_website, usrg_jobtitle, usrg_phone, usrg_address1, usrg_address2, usrg_state, usrg_postcode, usrg_country
Table 14: vlog-ops
vlop_id, vlop_title, vlop_description, vlop_tags, vlop_deadline, vlop_quantity, vlop_quantityposted, vlop_vser_id, vlop_usr_id,vlop_loc_name, vlop_loc_lat1, vlop_loc_lon1, vlop_loc_lat2, vlop_loc_lon2, vlop_campaign, vlop_rules, vlop_tips, vlop_status
So in main.php i have written the following Sql lookup
in main.php, I have the following SQL lookups:
$sql = "SELECT * FROM users_gor WHERE usrg_usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users_gor = $rows[0];
$sql = "SELECT * FROM users_vgr WHERE usrv_usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users_vgr = $rows[0];
$sql = "SELECT * FROM users WHERE usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
$sql = "SELECT * FROM vlog-ops WHERE vlop_usr_id ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
$sql = "SELECT usr_name AS vlop_usr_name FROM users WHERE usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
And then in the page template itself, I have written
<?php echo $vlop['vlop_vser_id'] ?>
<?php echo $vlop['vlop_usr_name'] ?>
The first one works, the second doesn’t. What I want eventually is to display the user name and the organisation name in a table.
Whenever I try a JOIN or a NATURAL JOIN or a LEFT JOIN it breaks and the entire site goes blank.
Any help for a newbie would be appreciated with a million thanks.
When you use JOIN you need to specify how you're joining them.
In the query below I'm assuming you're looking for the fields in bold from your question.
$query='SELECT u.usr_name, g.usrg_orgname, v.vlop_usr_id FROM users u
JOIN vlog-ops v on u.usr_id = v.vlop_usr_id
JOIN users_gor g on u.usr_id = g.usrg_usr_id';
I believe I got the name of the fields right but if not just replace them with the correct ones.
Once you have the data fetched, you just loop through the results:
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
echo 'User name = ' . $row['u.usr_name'];
echo 'Org name = ' . $row['g.usrg_orgname'];
echo 'Job posting user id = ' . $row['v.vlop_usr_id'];
}

How can I limit each user to just one like per page?

I have a liking system, a bit like what facebook have. However, users can like pages as many times as possible.. How can I limit it to just one for each user like per page? The code I have is:
<?php
session_start() ;
$conn = mysqli_connect("","","","");
$p_id = $_GET['post_id'];
$result = mysqli_query($conn, "SELECT * FROM forum WHERE post_id = $p_id");
$row = mysqli_fetch_assoc($result);
mysqli_query($conn, "UPDATE forum SET likes=likes+1 WHERE post_id = '$p_id'") ;
?>
<?php
header("Location: forum.php?id=".$row['post_id']);
die();
?>
You have to track the user. Make a table with userid and postid

How to grab an int from my MySQL server via PHP?

I am a novice when it comes to PHP but I don't understand if my syntax is wrong in this statement, or how would I grab an int from my MySQL server.
I know that my server credentials are working fine. How would I fix this statement to give me a returned integer of the number of reviews in the userinfo table?
$numberofpreviousreviews = mysql_query("SELECT `number_of_reviews` FROM `userinfo`") or die(mysql_error()); //Check to see how many reviews user has previously created
$amountofreviews = $numberofpreviousreviews + 1;
$query2 = mysql_query("ALTER TABLE userinfo ADD `amountofreviews` VARCHAR(10000)") or die(mysql_error()); //Make another column in database for the new review
You need to fetch your results after you run your query. There are several ways to do this but using mysql_fetch_assoc() will work for you.
$numberofpreviousreviews = mysql_query("SELECT `number_of_reviews` FROM `userinfo`") or die(mysql_error()); //Check to see how many reviews user has previously created
$row = mysql_fetch_assoc($numberofpreviousreviews);
$amountofreviews = $row['number_of_reviews'] + 1;
FYI, you shouldn't be using mysql_* functions anymore. They are deprecated and going away. You should use mysqli or PDO.
Assume you have a table userinfo which has the following structure and data :
Scenario #1 :
If you want to retrieve the all number_of_reviews, then do like this,
$query = "SELECT `number_of_reviews` FROM `userinfo`";
$result = mysqli_query($db,$query);
while ($row = mysqli_fetch_assoc($result)) {
echo "Number of reviews : " . $row['number_of_reviews'] . "<br/>";
}
It will give you,
Number of reviews : 20
Number of reviews : 40
Since, the result has many rows, it will display like above.
Scenario #2:
If you want to retrieve only the specific number_of_reviews for some user id (which is unique). I take id as 1 as a example here. Then do like,
$query2 = "SELECT `number_of_reviews` FROM `userinfo` WHERE `id` = 1";
$result2 = mysqli_query($db,$query2);
while ($row2 = mysqli_fetch_assoc($result2)) {
echo $row2['number_of_reviews'] . "<br/>";
}
This will print,
20.
Because, number_of_reviews is 20 for id 1.

I'm having issues with displaying this value from the database

...while connecting 2 table variables for use.
Here is what I have for query:
//GRABBING USER ID
if (isset($_SESSION['login_email'])) {
$session_entry = $_SESSION['login_email'];
$user_query = mysqli_query($connection, "SELECT id FROM users WHERE email = '$session_entry'");
$user_assoc = mysqli_fetch_assoc($user_query);
$user_id = $user_assoc;
}
//GRABING ACCESS ID
if (isset($_SESSION['login_email'])) {
$access_query = mysqli_query($connection, "SELECT access FROM user_access WHERE user_id = '$user_id'");
$access_assoc = mysqli_fetch_assoc($access_query);
$access = $access_assoc;
}
and the $access echo's "Array"... Don't know what to do about it.
Right now, in my user_access table I have 2 columns: 1. user_id and 2. access
I want it to echo out the access code, but like said above, all I get is "Array".
I attend on using this for the purpose of giving users access codes for accessing specific webpages that they have permission to access through these codes.
$access = $access_assoc;
must be
$access = $access_assoc['access'];
Also
$user_id = $user_assoc;
must be
$user_id = $user_assoc['id'];
Check mysqli_fetch_assoc Manual

Friend News feed - Recent Updates

I have an a users friends stored in the database as friends_array. They store the id of the friends and are separated with coma's.
I was able to output the posts made by the users but i have a problem. This is my code.
if ($friend_feed != "") {
$feed= explode(',', $feed);
foreach ($feedas $key => $value) {
$query = mysql_query("SELECT * FROM `users` WHERE user_id=$value ORDER BY `time` DESC") or die ("Please try again later.");
while ($row = mysql_fetch_array($query)) {
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$username = $row['username'];
$post = $row['post'];
echo '<hr>'.$first_name.' '.$last_name.' <br>'.$post;
}
}
}
This code above goes through the friend id's and outputs the posts...but it goes by each friend first...
For example i have 3 friends with the id's 70,71 and 72. If all of them make 2 posts each, they are outputted in the order of their id stored in the database, so user 70's posts will come first and then all of user 71's and then user 72...
How do i stop this and output by what post is recently put into the database?
You can try a different query, like:
SELECT * FROM `users` WHERE user_id IN ($feed) ORDER BY `time` DESC
and drop the whole foreach and explode

Categories