I made this more understandable what i want here, so:
I want to SELECT the users here that has the ,for example , "coder" in his "proffesion" field, in Mysql table. Because as you see here by this code it will pick a random user except the user who is logged in. And i need to make another condition after AND ( see the code line 2 ) so it FINDS and puts out only user with the "coder" written in proffesion field. What info do you need more to help to solve the problem?
ps. it should pick from column - PROFESSION
public function whoToFollow1($user_id, $profileID){
$stmt = $this->pdo->prepare("SELECT * FROM `users` WHERE `user_id` != :user_id AND (what stetement need to be here) ORDER BY rand() LIMIT 1");
$stmt->execute(array("user_id" => $user_id));
$users = $stmt->fetchAll(PDO::FETCH_OBJ);
echo '<div class="wrap"><div class="inner"><div class="title"></div>';
foreach ($users as $user) {
You just need to add the condition you described to the query i.e.
$profession = "coder";
$stmt = $this->pdo->prepare("SELECT * FROM `users` WHERE `user_id` != :user_id AND profession = :profession ORDER BY rand() LIMIT 1");
$stmt->execute(array("user_id" => $user_id, "profession" => $profession));
// I am assuming you have profession table with id and profession name/type
SELECT *
FROM `users`
INNER JOIN profession on users.profession_id = profession.id
WHERE `user_id` = :user_id
AND profession.id = :profession_id
$stmt = $this->pdo->prepare("SELECT * FROM `users` WHERE `user_id` != :user_id AND `profession`=:job ORDER BY rand() LIMIT 1");
$stmt->execute(array("user_id" => $user_id, "job" => $job));
Note: $job will be used to represent the current job you wish to target.
Related
So i have 2 queries, I am trying to run one inside of a loop (unsuccessfully), but I'm thinking there might be a possibility that I can combine the 2 of them.
Table profile_posts
ID
post_title
post_body
user_id
post_date
Table profile_posts_likes
ID
likes
user_like_id
post_id
$baseURL = 'Data.php'; //Grab page for pagination
$id = $_GET['id']; //Grab id from link
$limit = 10; //Limit returns to 10
//Select statement that grabs results from profile_posts table
$postQuery = "SELECT * FROM profile_posts WHERE user_id = :id ORDER BY post_date DESC LIMIT $limit"; //First Query
// Count of all records
$rowCount = countRecords($conn, $id);
// Initialize pagination class
$paginationConfig = array(
'postID' => $id,
'baseURL' => $baseURL,
'totalRows' => $rowCount,
'perPage' => $limit,
'contentDiv' => 'postContent',
'link_func' => 'searchFilterProfile'
);
$pagination = new Pagination($paginationConfig);
$profilePost = $conn->prepare($postQuery);
$profilePost->bindParam(":id", $id);
$profilePost->execute();
if($profilePost->rowCount() > 0){
foreach($postDisplay as $row){
$likeQuery = "SELECT id, COUNT(likes) as likeCount, user_Like_id, post_id FROM profile_posts_likes WHERE post_id = :postID"; //Second Query
$likeQuery = $conn->prepare($likeQuery);
$likeQuery->bindParam(":postID", $row['id']); //Grab id from first query
$likeQuery->execute();
$postlikeQuery = $likeQuery->fetchAll();
if($likeQuery->rowCount() > 0){
//Display like buttons, when user clicks "Like" button, send data through Ajax
//and update page with "Liked"
}
}
}
What this does is displays the posts on the users profile page, and then when a user views their page, they can 'Like' the post or 'unlike it'...Using Ajax to update page
Now is there a way that I can combine those 2 queries together instead of running one inside of the loop. I tried tossing a WHERE EXISTS in there to combine the Select statements, but no luck.
Appreciate any help. Thanks in advance.
You may express your query using a join:
SELECT ppl.id, ppl.user_Like_id, ppl.post_id
FROM profile_posts_likes ppl
INNER JOIN
(
SELECT *
FROM profile_posts
WHERE user_id = :id
ORDER BY post_date DESC
LIMIT $limit
) pp
ON pp.id = ppl.post_id;
Selecting COUNT in the second query makes no sense, as you are not using GROUP BY.
I've been trying to get a query that would help me to display conversation list like any chat web app, I want to mix the sent messages(I mean conversations) with received messages even if the other user has not replied it. and only show the last conversation made.
I am using PHP here is the query I tried.
I Have a message table and users table
above message table, below user table
public function conversations($user_id){$stmt = $this->pdo->prepare("SELECT * FROM `messages` LEFT JOIN users ON `messageFrom` = :user_id OR `messageTo` = :user_id AND `messageID` IN (SELECT max(`messageID`) FROM `messages` WHERE `messageFrom` = :user_id OR `messageTo` = :user_id) WHERE `messageTo` = :user_id and `messageFrom` = user_id or `messageFrom` = :user_id and `messageTo` = `user_id` GROUP BY messageTo, messageFrom ORDER BY `messageID` DESC ");
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
I want to combine my two SELECT statements with an if statement. Currently I am just getting the row and then doing if($row['activated'] === 1) { ... run 2nd select statement }.
There has to be a better and much faster way of doing it (performance-wise). This is what I'm trying to do:
For example: (1st SELECT statement)
$db = $stmt->("SELECT * FROM users WHERE id = :id AND activated = 1");
$db->bindParam(':id', $id);
$db->execute();
If that above SELECT statement is true, then do this: (2nd SELECT statement)
$db = $stmt->("SELECT * FROM user_settings WHERE user_id = :user_id");
$db->bindParam(':user_id', $id);
$db->execute();
How can I combine those two SELECT statements? If it is else or if activated = 0, then only do the first select.
Bonus question:
How can I make it three If statements? For example, the 2nd SELECT statement will be like this:
$db = $stmt->("SELECT * FROM user_settings WHERE user_id = :user_id AND color = :color");
$db->bindParam(':user_id', $id);
$db->bindParam(':color', $color);
$db->execute();
Now if that statement is true, run this 3rd SELECT statement:
$db = $stmt->("SELECT * FROM friends WHERE user_id = :user_id");
$db->bindParam(':user_id', $id);
$db->execute();
You can merge/combine
SELECT * FROM user_settings WHERE user_id = :user_id AND color = :color
and
SELECT * FROM friends WHERE user_id = :user_id
Into one query with a JOIN
SELECT
*
FROM
user_settings
INNER JOIN
friends
ON
user_settings.user_id = friends.user_id
AND
user_settings.color = :color
Or a other JOIN format
SELECT
*
FROM
user_settings
INNER JOIN
friends
ON
user_settings.user_id = friends.user_id
WHERE
user_settings.color = :color
Or you can write it as this JOIN. (most likely slower)
SELECT
*
FROM (
SELECT
*
FROM
user_settings
WHERE
user_settings.color = :color
) AS user_settings
INNER JOIN
friends
ON
user_settings.user_id = friends.user_id
The used method which is best/faster can/will be depending which indexes you have in use.
I've successfully managed to output the posts of a users friends...
$query = mysql_query("SELECT * FROM `users_posts` WHERE user_id IN ($friend) ORDER BY `time` DESC");
This outputs all of the friends posts of the user. However I want to get the posts of the user itself AS WELL as the friends posts...this is what I have tried...
$query = mysql_query("SELECT * FROM `users_posts` WHERE user_id IN ($friend) AND `user_id`=$session_user_id ORDER BY `time` DESC");
I even tried making another mysql statement just to load the users posts however when I did that, the users posts were being outputted first in the order of the time that they were outputted and then after the users posts were the friends posts shown...
Thanks :)
You actually want an OR condition:
$query = mysql_query("SELECT * FROM `users_posts` WHERE user_id IN ($friend) OR `user_id`=$session_user_id ORDER BY `time` DESC");
Try:
$query = mysql_query("SELECT * FROM `users_posts` WHERE user_id IN ($friend) OR `user_id`=$session_user_id ORDER BY `time` DESC");
That will return anything where user_id is in your array; as well as the user's own ID.
If I understand correctly chwhat you are trying to do all you need do is change
And 'user_id = $session_iser_id
To
Or 'user_id = $session_iser_id
<?php
include('includes/config.php');
$topi = $_GET['id']; //id of url
mysql_select_db("ban", $con);
$query = "SELECT * FROM `basic` WHERE id = '$topi' LIMIT 0, 30";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$aa = $row['item'];
$cc = $row['moreinfo'];
$dd = $row['contactinfo'];
$ff = $row['id'];
In this script, I get information from the table basic, but I want to retrieve data from another table named users. How can I retrieve data from two tables at once?
users table consists of following columns:
email
username
ID
You need to JOIN the two tables on a common value, called a foreign key. Once you've posted the structure of the users table as requested in the comments, I can provide a more complete example.
EDIT: See example. This calls explicit column names instead of SELECT *.
$query = "SELECT
basic.id,
basic.item,
basic.moreinfo,
basic.contactinfo,
users.email,
users.username
FROM basic JOIN users ON basic.id = users.id
WHERE id = '$topi'
LIMIT 0 , 30";
You would use a JOIN onto the other table.
$query = "SELECT *
FROM basic b
JOIN users u ON b.user_id = u.user_id
WHERE id = '$topi'
LIMIT 0, 30";
Something like that, but based on your fields.
Please Note: the ON clause specifies what you will be looking for a match on.