PHP MySQL query help - php

I am trying to use this query to return every instance where the variable $d['userID'] is equal to the User ID in a separate table, and then echo the username tied to that user ID.
Here's what I have so far:
$uid = $d['userID'];
$result = mysql_query("SELECT u.username
FROM users u
LEFT JOIN comments c
ON c.userID = u.id
WHERE u.id = $uid;")$row = mysql_fetch_assoc($result);
echo $row['username'];

This should work
$uid = mysql_real_escape_string($d['userID']);
$result = mysql_query("SELECT u.username
FROM users u
LEFT JOIN
comments c
ON c.userID = u.id
WHERE u.id = '$uid'");
while($row = mysql_fetch_assoc($result))
{
//PRINTS ALL INSTANCES OF THE ROW
echo $row['username'];
}
what is printed from the echo above.

if you're looking to get all the results. wrap your mysql_fetch_assoc in a while loop:
$result = mysql_query("SELECT u.username
FROM users u
LEFT JOIN comments c
ON c.userID = u.id
WHERE u.id = $uid;");
while($row = mysql_fetch_assoc($result)){
echo $row['username'];
}

Try this:
$uid = $d['userID'];
$result = mysql_query("SELECT u.username
FROM users u
LEFT JOIN comments c
ON c.userID = u.id
WHERE u.id = $uid");
while($row = mysql_fetch_assoc($result))
{
echo $row['username'];
}
*EDIT:*Removed semicolon.

Related

Failing to write a complex condition

I want to display the comments of the post that has a commnet from the logged in user.
Suppose I commented on a facebook post and with my if condition I want to display later comments of the same post.
The logged in user id = $id
I tried this condition:
if($comment_uid== $id || $row['p_post_id'] = $row['c_post_id'])
Currently its displaying all the rows
sql = "SELECT DISTINCT
c.post_id AS c_post_id,
c.comment_id,
c.comment,
c.user_id AS c_uid,
u.username,
u.dp,
c.is_marked,
p.user_id AS p_uid,
p.post_id AS p_post_id
FROM comment c
INNER JOIN user u ON c.user_id = u.id
INNER JOIN post p ON c.user_id = p.user_id
ORDER BY c.comment_id DESC";
$result = mysqli_query($con, $sql);
if ($result) {
while ($row=mysqli_fetch_assoc($noti_result)) {
$post = $row['c_post_id'];
$comment = $row['comment'];
$username = $row['username'];
$comment_uid = $row['c_uid'];
$post_uid= $row['p_uid'];
if($comment_uid== $id || $row['p_post_id'] = $row['c_post_id'])
{
echo $username also said $comment</a><br>";
}

Three SELECT and two jsons

I created a SELECT to get my communities.
And create two SELECTs to get the communities I'm following.
But I get just my communities.
I do not get the communities I'm following.
$user_id = $_GET["id"];
$row1 = array();
$row2 = array();
// get my communities
$res1 = mysql_query("SELECT * FROM communities where user_id = '$user_id'");
while($r1 = mysql_fetch_assoc($res1)) {
$row1[] = $r1;
}
// get "id" of my communities I'm following
$res = mysql_query("SELECT * FROM communities_follow where user_id = '$user_id'");
while($r = mysql_fetch_assoc($res)) {
$coid = $r["coid"];
// get my communities I'm following
$res2 = mysql_query("SELECT * FROM communities where id = '$coid'");
while($r2 = mysql_fetch_assoc($res2)) {
$row2[] = $r2;
}
}
$resp = array_replace_recursive($row1, $row2);
print json_encode( $resp );
The inner join will get you those communities only, where you are following:
SELECT c.* FROM communities c
INNER JOIN communities_follow cf ON c.id = cf.coid
WHERE cf.user_id = '$user_id';
Or, without a JOIN:
SELECT * FROM communities
WHERE EXISTS (SELECT 1 FROM communities_follow cf
WHERE c.id = cf.coid AND cf.user_id = '$user_id')
Try this sql.
SELECT * FROM communities c LEFT JOIN communities_follow cf ON c.user_id = cf.user_id where
cf.user_id = '$user_id';

Using php if...else statement with two queries

I have two queries that count the number of data for both "artists" and "groups" in my database. I want to display a message if there is data to display for either artists or groups (or both), and if the data returns 0 for both of them then not to display anything.
I have the following code which doesn't seem to work:
<?php if (($numrowsartists==0)OR($numrowsgroups==0)) {
} else {
echo "There is information to display.";
}
?>
Below are the queries I have:
$query = "SELECT COUNT(*) FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
WHERE c2a.song_id = $id";
$res = mysql_query($query);
$numrowsartists = mysql_fetch_assoc($res);
$query = "SELECT COUNT(*) FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist_Group` AS ag ON ag.group_id = c2a.group_id
WHERE c2a.song_id = $id
ORDER BY ag.group_name ASC";
$res = mysql_query($query);
$numrowsgroups = mysql_fetch_assoc($res);
Thanks in advance. I'm sure it's probably a super basic fix but I'm still very new to php and would appreciate some help.
You should getthe value frorm the row eg using alias for column name
$query = "SELECT COUNT(*) as num_artists FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
WHERE c2a.song_id = $id";
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
$numrowsartists = row['num_artists'];
$query = "SELECT COUNT(*) as num_groups FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist_Group` AS ag ON ag.group_id = c2a.group_id
WHERE c2a.song_id = $id
ORDER BY ag.group_name ASC";
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
$numrowsgroups = row['num_groups'];
There are several solutions, the easiest being the following:
if($numrowsartists[0]+$numrowsgroups[0] > 0)
However, as people have said, you shouldn't use mysql_* functions anymore.
Assuming the ID is user input, you should really use prepared statements.
Also, you can handle both tests in a single query:
$stmt = $con->mysqli_prepare("SELECT COUNT(1) as `count` FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
INNER JOIN `Artist_Group` AS ag ON ag.group_id = c2a.group_id
WHERE c2a.song_id = ?");
$stmt->bind_param('i',$id);
$stmt->execute();
if($stmt->get_result()->fetch_array()[0] > 0){
...
}else{
//message that nothing was found
}

MYSQLI SELECT with Limit not working

I'm trying to find the number of rows in a table in the database depending on certain conditions with a limit feature so that i can count the number of rows there are matching the condition after a certain row in the table.
Thus i created my php query:
$q = $db->query("SELECT u.*, f.* FROM updates U LEFT JOIN friends f ON f.fid = u.userid WHERE f.uid = '$userid' ORDER BY u.up_id DESC LIMIT $limitID, 9999999");
$nr = $q->num_rows;
However even if there are more rows in the database after the $limitID, it says there are no rows.
If I try this:
$q = $db->query("SELECT u.*, f.* FROM updates U LEFT JOIN friends f ON f.fid = u.userid WHERE f.uid = '$userid' ORDER BY u.up_id DESC LIMIT $limitID");
$nr = $q->num_rows;
then it works, but it doesnt count after the $limitID. Any udeas?
According yo your query, it should be like
$sql = "SELECT count(*) FROM updates U
LEFT JOIN friends f ON f.fid = u.userid WHERE f.uid = ?";
// getting stuff using prepared statements
$row = $res->fetch_row();
$num = $row[0] - $limitID;
but I doubt it's really what you need either.
Using what Your Common Sense told me I managed to find an answer to my problem, so here it is:
$limitID = $upid;
$sql = "SELECT count(*) FROM updates u
LEFT JOIN friends f ON f.fid = u.userid WHERE
(f.uid = '$userid' AND u.up_id > '$limitID')";
$res = $db->query($sql);
$row = $res->fetch_row();
$num = $row[0];
if($num == 0){
// do nothing
} else {
echo "<span class='newPosts'>" .$num. " New Post"
. (($num>1)?'s':' ') . "</span>";
}
works perfectly

Is this query for selecting MUTUAL FRIEND is CORRECT?

<?php
mysql_connect("localhost","root","");
mysql_select_db('db2012');
$uid = 8;
$mid = 10;
$q = mysql_query("select friend from users_friends where user = $uid") or die(mysql_error());
if(mysql_num_rows($q) > 0)
{
while($r=mysql_fetch_array($q))
{
$qq = mysql_query("select friend from users_friends where user = $mid") or die(mysql_error());
while($rr = mysql_fetch_array($qq))
{
if($r[friend]==$rr[friend])
{
$friend_name_query = mysql_query("select name from users where uid = '$rr[friend]'") or die();
$friend_name = mysql_fetch_array($friend_name_query);
echo $friend_name[name];
}
}
}
}
?>
This query is working but any other way out to use less queries ? i m a beginner programmer please let me know if there is n e thng ...
You should use just one query:
$query = 'SELECT u.name
FROM users_friends f1
INNER JOIN users_friends f2 ON (f2.friend = f1.friend)
INNER JOIN user u ON (u.uid = f2.friend)
WHERE f1.user = "'. $uid. '"
AND f2.user = "'. $mid .'"';
run the query
$q = mysql_query($query) or die(mysql_error());
and do the while loop
while($r=mysql_fetch_array($q)){
echo $r['name'];
}
You only need the if(mysql_num_rows($q) > 0) statement if you do an else, otherwise you can just use the while
You could try below in order to get mutual friends of user 8 and 10.
select * from
user_friends a inner join user_friends b on
a.friend=b.friend and (a.user='8' and b.user='10')
U could then join the user table to get the name.

Categories