php like concat get userid with username - php

I am creating PHP user details display script I have added userid to Like Conact method but i want to add userid and username to it's two parameters
Here is my code
if(isset($_GET["user_id"]))
{
$uid = $_GET["user_id"];
}
if ($stmt = $con->prepare("SELECT p.*, c.cat_title,u.username,u.profile_pic,u.
content_details,u.web_link,u.fb_page,
u.github,u.twit_link,title,desc1,time,img,
views,id FROM products AS p
JOIN users AS u ON u.user_id= p.user_id
JOIN category AS c ON c.cat_id = p.cat_id
WHERE u.user_id LIKE CONCAT('%',?,'%') ")){
$stmt->bind_param("s", $uid);
$stmt->execute();
}

WHERE u.user_id LIKE CONCAT('%',?,?,'%')")){
$stmt->bind_param("ss", $uid, $username);
Is this what you were asking for? Based on your question this would do it. If you're trying to do LIKE by either or then you'll have to use OR and do another LIKE comparison.

Related

getting all posts the user has liked from database

I am trying to get all the posts the user has liked displaying and not all posts.
I have database tables as follows:
likes (columns (int): likeID, likeBy, likeOn)
posts (columns: postID, postBy, text, likeCount)
users (Columns: userID, username)
Here is my code to get the users liked post (at the moment it gets all liked posts on database and not user liked posts)
public function likes($user_id, $num){
$stmt = $this->pdo->prepare("SELECT * FROM `likes`, `posts`, `users` WHERE `likeOn` = `userID` and `user_id` = `postBy` ORDER BY `likeOn` DESC");
$stmt->bindParam(":num", $num, PDO::PARAM_INT);
$stmt->execute();
$tweets = $stmt->fetchAll(PDO::FETCH_OBJ);
any ideas on how to get the posts liked by the user and not all liked posts?
select p.postID from posts p inner join likes l on l.likeOn=p.postID inner join users u on u.userID = l.likeBy;
I think the above query will work for you try it in MySQL database.
As others have said in comments, your schema is not structured in a proper way to have solid relations between tables.
But, given your current structure (and assuming you are updating the likes count, and that posts.postBy contains the user id), you could try something like:
SELECT posts.* FROM posts AS p WHERE COUNT(p.likeCount) > 0 AND p.postBy = the_user_id_variable
If you need posts only, and the userId is contained inside the post row, you do not need to join different tables.
I think this will work.
I changed your query so the statement will return the posts and likes coupled together with the inner join
public function likes($user_id, $num){
$stmt = $this->pdo->prepare("SELECT * likes INNER JOIN posts ON likes.likeOn = posts.postID WHERE likes.likeBy = ? ORDER BY `likeOn` DESC");
$stmt->bind_param("i",$user_id);
$stmt->bindParam(":num", $num, PDO::PARAM_INT); // Don't know what this does
$stmt->execute();
$tweets = $stmt->fetchAll(PDO::FETCH_OBJ);
}
Edit: removed the User table from the query because the userid allready existed in the like table (likeBy).

PHP, How to display the 2nd table

DBOperations.php
public function getByQRID($id){
$stmt = $this->conn->prepare("SELECT students.*, courses.* FROM students INNER JOIN courses ON courses.id = students.course_id WHERE students.id = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
Update the code
If you are you using MySql, then try this:
select * from Student
inner join Course on Student.course_id = Course.id
where Student.id = 1
(1 it's an example, and I'm guessing things, since your question wasn't entirely clear).
I hope it helps!

PHP MySQLi get columns from foreign key

I have the following tables:
'auktionen'
id|uid|typ(FK)|min|max|menge|...
'typen'
id|typ
1|Hack
2|Schredder
where typ in typen is just a textutal representation, which I would like to get.
$prep_stmt = "SELECT anzeigentyp, typ, holzart,
qualitaet, rinde, min, max, menge FROM auktionen WHERE uid = ?";
$stmt = $mysqli->prepare($prep_stmt);
$stmt->bind_param('i', $user_id);
$stmt->execute();
$stmt->bind_result($anzeigentyp, $typ, $holzart, $qualitaet, $rinde, $min, $max, $menge);
$stmt->store_result();
So when fetching all my results I want to get "Hack" instead of the referencing id (in case it is 1). I guess it needs some JOIN to be achieved and I tried it like this without success:
$prep_stmt = "SELECT anzeigentyp, typen.typ, holzart,
qualitaet, rinde, min, max, menge FROM auktionen WHERE uid = ?
JOIN typen ON auktionen.typ = typen.typ";
What is the proper way to do it?
You must move the where clause to the end and join the corresponding columns typen.id and auktionen.typ
select a.anzeigentyp, t.typ, a.holzart, a.qualitaet, a.rinde, a.`min`, a.`max`, a.menge
from auktionen a
join typen t on t.id = a.typ
where ...

How to fetch results from two tables? (MySQLi)

How do I retrieve results from two tables in MySQLi using the object oriented way?
I am trying to fetch user information for $stmt2 from $stmt. $stmt loads perfectly but $stmt2 doesn't because it isn't getting the results.
Here's the code for it:
<?php
if ($stmt = $conn->prepare('SELECT `id`, `kills` FROM `rp_stats` ORDER BY `kills` DESC LIMIT 6'))
{
$stmt->execute();
$stmt->bind_result($id, $kills);
while($stmt->fetch())
{
if ($stmt2 = $conn->prepare("SELECT `id`, `username`, `look`, `online` FROM `users` WHERE `id` = ?"))
{
$stmt2->bind_param("i", $id);
$stmt2->execute();
$stmt2->bind_result($id, $username, $look, $online);
$user = $stmt2->get_result();
}
global $stmt2;
echo '<div class="leaderboardWrap">
<div class="userAvatar" style="float: left;width: 50px;display: inline-block;height: 50px;background-image: url(\'https://www.habbo.nl/habbo-imaging/avatarimage?figure='. $look . '&size=m&headonly=1\');"></div>
<div class="leaderboardContainer">
<p style="padding-top: 6px;"><span class="username-rainbow"><a ng-click="progress()" href="/user/' . $username . '">' . $username . '</a></span></p>
<p style="margin-top: -9px;"><i>$' . formatWithSuffix($credits) . ' cash</i></p>
</div>
</div>';
}
$stmt->close();
}
?>
I want it to show 6 different users based on the user stat as the left image shows, but instead, I am getting the right image.
Try to use inner join in database to minimize fetch process of data:
SELECT `id`, `kills` FROM `rp_stats`
INNER JOIN users ON rp_stats.id = user.id
ORDER BY `kills` DESC LIMIT 6
You're using same column names in different tables, so a join conflicts by default. That's why you must use aliases, like so:
SELECT s.id, s.kills, u.username
FROM rp_stats s
INNER JOIN users u ON s.user_id = u.id
ORDER BY s.kills DESC LIMIT 6
Note! that I use user_id. This is the foreign key (reference) from rp_stats to users. I mean it's not correct to compare users.id with rp_stats.id. Using this construction, a user can always have max. 1 stat. You generate more flexibility to user a user_id column, which references to the user table.
"SELECT RS.`id`, RS.`kills`, US.`id`, US.`username`, US.`look`, US.`online` FROM rp_stats RS, users US WHERE US.`id`=RS.`id` ORDER BY RS.`kills` DESC LIMIT 6";
A normalized query could help out.

php mysql select something and get columns from other tables

I'm using prepared statements and I need to "select" other table, apart from these two, to get data but I get this:
Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\views\user\referral.php on line 16
If I add in SELECT table1.* , table.* , "theothertable.*"
$stmt = $mysqli->prepare("SELECT friends.*, rc_usuario.* // or just *
FROM friends
INNER JOIN rc_usuario ON rc_usuario.id = friends.friendID
WHERE friends.userID = ?");
$stmt->bind_param('s', $connectedUserID);
This is working fine, I get what i need, but I also need to get data from another table and I can't make other select because i need it all in a while to print all the data together.
The question is, can I SELECT something like that from 2 tables and also get data from other table/s?
Thank YOU!
EDIT: Add the new statement:
if ($stmt = $mysqli->prepare("SELECT friends.*, members.*, account_type.*
FROM friends
INNER JOIN members ON members.id = friends.friendID
INNER JOIN account_type ON account_type.name = members.acc_type
WHERE friends.userID = ? AND members.acc_type = ?")) {
$stmt->bind_param('is', $connectedUserID, $connectedAcc_type);
$stmt->execute();
} else echo $mysqli->error;
You can join more tables by using another INNER JOIN, like as follows;
INNER JOIN rc_usuario ON rc_usuario.id = friends.friendID
INNER JOIN rc_another ON rc_another.col = friends.coljoin
Just make sure you select all the columns you want in the joined table.
It might also help to run your prepare statement in an if, like this;
if($stmt = $mysqli->prepare("SELECT ...")) { // ... where the rest of your query is
$stmt->bind_param('s', $connectedUserID);
$stmt->execute();
}
else {
echo $mysqli->error;
}
which will give you an idea of any problems with the SQL syntax.
Hope this helps.

Categories