What I'm trying to do is take a whole table and find out what the id is of the member whose email address is in the value of emails.from and put their id in there instead. I'm trying to improve my performance by using IDs instead of email addresses but am unable to complete this task.
$mysql = new mysql();
$mysql->query('SELECT `from` FROM `emails` ORDER BY `id` ASC' );
while($row = mysql_fetch_array($mysql->result)){
$mysql2 = new mysql();
$mysql2->query('SELECT `id` FROM `exchange` WHERE `email` = "'.$row['from'].'"');
$details = mysql_fetch_assoc($mysql2->result);
$mysql2->query('UPDATE `emails` SET `from` = '.$details['id'].' WHERE `from` = "'.$row['from'].'"');
}
No need to look it up if you're doing the whole table.
UPDATE emails SET from=id
Will do each row in the table.
If I understand your code, you could try something like this:
UPDATE `emails`
INNER JOIN `exchange` ON `exchange`.`email` = `emails`.`from`
SET `emails`.`from` = `exchange`.`id`
The query below should do the trick :)
update emails
set `from`=(select e.id from exchange as e where e.email=em.`from`)
from emails as em
order by em.id asc
Related
I wanted to fetch data from MySQL with PHP when a user inputs something. I have this code.
$query = "SELECT * FROM mealplans WHERE plantitle LIKE '%$inputText%' AND `STATUS` = 1 OR plantitle LIKE '%$inputText%' AND userid = '$userid' GROUP BY plantitle, userid";
$result = mysqli_query($con, $query);
It works fine however, it fetches the oldest or first data from the group. How do I select the newest data?
I've search and found possible solutions by using FROM, JOIN, IN, etc. However, I do not know how to use them since I only know the basics. Hopefully someone could explain one solution for me.
There is a simple solution that you can do.
You can order your data to DESC order. I'm sure that you have an id(primary key) in your table.You just have to order all your datas' to DESC order of ids .So that your last inserted data set will be the one to be fetched since it's on the top.
change your query to: (add your primary key(in the following query i have added it as id) in the ORDER BY syntax)
$query = "SELECT * FROM mealplans WHERE plantitle LIKE '%$inputText%' AND `STATUS` = 1 OR plantitle LIKE '%$inputText%' AND userid = '$userid' GROUP BY plantitle, userid ORDER BY `ID` DESC"
Just add a condition in your query which only retrieves the row having the greatest id.
Find out the id first and then you can use the same query for that id:
SELECT MAX(id) FROM mealplans WHERE plantitle LIKE '%$inputText%' AND `STATUS` = 1 OR plantitle LIKE '%$inputText%' AND userid = '$userid' GROUP BY plantitle, userid
After getting the id and storing it in a variable (say $temp):
SELECT * from mealplans where `id` = $temp
I was trying to build a chat plugin in my site but I got problem while sorting the incoming messages in the Inbox. When I use the code below, it doesn't order the messages according to new sent but orders according to history of sent messages:
E.g. if I send message to "A" at first, "B" after that and "C" at last, the code I works fine up to here. It shows C at Top, B at middle and A at end. But When I again send message to B, the "B" doesn't come up at the top.
Edit:
Someone tagged this question as duplicate. The problem about that question is abit different. I already did extract the unique row (as asked by that question) but I cannot sort it according to time.
Here is the code I use and please ignore the mysql_* tag I used here. It is just for testing.
<?php
$sql = mysql_query("SELECT DISTINCT `from` FROM `message` WHERE `to`='$username' ORDER BY `time` DESC");
// using loop
while($row = mysql_fetch_assoc($sql)){
echo $row['from'];
echo "<br/>";
}
?>
You are ordering by a not selected column
if you want order by time i think you should select this column :
try
$sql = mysql_query("SELECT `from` , max(`time`) FROM `message`
WHERE `to`='$username' GROUP BY `from` ORDER BY `time` DESC " );
Grouping by not require distinct
For messages you can select rows ordered by id desc like this:
SELECT * FROM `message` WHERE `to`='username' ORDER BY id DESC
Sender list you can get like this:
SELECT MAX(`id`),`from` FROM `message` WHERE `to`='username' GROUP BY `from` ORDER BY MAX(`id`) DESC
Do not order messages by time.
The best practice is to having unique autoincrement id column in message table and ordering messages by they's ids
I'm trying to change my user's news feed to only show posts from friends, instead of from all users on the site. I managed to do so doing something like this:
function friend_posts(){
global $session_user_id;
if(friends_exist($session_user_id) === true){
$friends = mysql_query("SELECT * FROM `friendship` WHERE `user_id` = $session_user_id AND `pending` = 0");
while($friend = mysql_fetch_array($friends)){
$friendID = $friend['friend_id'];
$posts = mysql_query("SELECT * FROM `posts` WHERE `userid` = $friendID ORDER BY `timestamp` DESC");
while($post = mysql_fetch_array($posts)){
$friendData = user_data($post['userid'],'username','first_name','last_name');
echo $friendData['username'].": ".$post['status']."<br>";
}
}
} else {
echo "Nothing to display. Try adding some friends!";
}
}
However, this isn't that convenient. For example, if I want to paginate, I don't know how I'd even start to do that using something like this. Also if multiple users post, how would I sort them by 'timestamp' descending?
I'm guessing the only route I can take is accessing columns from multiple tables somehow, then sorting by the timestamp, which is stored in the posts table. The friendship table just has id, user_id, friend_id and pending.
How would I go about doing that? Is there an easier way to do what I'm trying to do?
I'm very new to SQL. I don't know too much other than inserting/deleting/updating.
You could use a single statement like this:
SELECT * FROM posts WHERE userid in
(SELECT friend_id FROM friendship WHERE user_id = $session_user_id AND pending = 0)
ORDER BY `timestamp` DESC
This way you get only the posts of the friends of the current user. If you also need data from the friendship table then use a join.
my php script save list of userid in a SESSION ARRAY variable for the purpose of creating html link on that page.
when we click that html link another PHP script should display all user information for that userid in a html table.
I need yo know best practice to create SELECT query for this
1) SELECT * from `users` WHERE
`user_id` = '$array_valuer1' OR
`user_id` = '$array_valuer2' OR
`user_id` = '$array_valuer3' OR
`user_id` = '$array_valuer4' OR
`user_id` = '$array_valuer5' OR
`user_id` = '$array_valuer6' OR
----
----
`user_id` = '$array_valuern';
2) select all user --> SELECT * from `users`
then using php script display only the userid in the SESSION array and skip other userid
which is best practice? the SESSION array is temporary and i do not to want to save in a database.
thanks
If you are storing few user ids in a session its okay. But if the number is huge dont store them in SESSION. Its better to fetch them using a query.
About the query you can use following form.
$q = "SELECT * from `users` WHERE `user_id` IN ('".implode("','", $array)."')";
shiplu.mokadd.im answer is good, but using PDO is better:
$db = new PDO("...");
$statement = $db->prepare("SELECT * from `users` WHERE `user_id` IN (':list')");
$statement->execute(array(':list' => implode("','", $array)));
$row = $statement->fetch();
I have a reports table where i count hits by just using the code below.
// Get hits
$hits_query = mysql_query("SELECT COUNT(*) AS `count` FROM `reports` WHERE `stuff_id` = ".$row->id."");
$fetch_hits = mysql_fetch_array($hits_query);
$hits = $fetch_hits['count'];
That code works great and does exactly what i want but what i want now is how to get unique hits?
In my reports table i store every access so theres multiple log for each user base on the stuff_id . In my reports table I store the date as 01-23-2013 on each report, along with the user ip and stuffed which every 24 hours change from 2 to 1.
You need a field that uniquely identifies the user. I am going to assume for my example that it is user_ip. Change this:
$hits_query = mysql_query("SELECT COUNT(*) AS `count` FROM `reports` WHERE `stuff_id` = ".$row->id."");
To this:
$hits_query = mysql_query("SELECT COUNT(DISTINCT `user_ip`) AS `count` FROM `reports` WHERE `stuff_id` = ".$row->id."");
NOTE: mysql library will be deprecated as of PHP 5.5 and removed in future versions of PHP. It would be a good idea to start planning a migration away from it.
you can do this:
$hits_query = mysql_query("SELECT COUNT(DISTINCT `user_ip`) AS `count` FROM `reports` WHERE `stuff_id` = ".$row->id."");
Or if you want a unique response, you can do this:
$user-IP = "Put here the ip adress of the user that you want";
$hits_query = mysql_query("SELECT COUNT(*) AS `count` FROM `reports` WHERE `user_ip` = ".$user-IP."");