PHP - Grabbing info from the database (complicated) [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need a little help. I need to grab all rows which contain their logged in ID from a database. Basically, I have a column in the database called "owners" and in it it has a few IDs. Lets say "1, 6, 8", If user ID 6 is logged in, I need to grab that row. If it doesn't contain 6, say "2, 5, 7" then don't grab it. My wording probably isn't good.
--------------------------------
ID |Name |Other Info |Owners|
---|--------|-----------|------|
1 |Testing |Testing 123|1,4,6 |
---|--------|-----------|------|
2 |Testing1|Catz |5,7,8 |
---|--------|=-----------------|
3 |Testing2|Woof |6,9,10|
---|--------|-----------|------|
In other words:
User ID 6 logs in.
Goes to page which needs the rows to be grabbed.
Script grabs rows 1 and 3 (because the owners list contains 6).
PHP foreach, showing the resutls in a table. (Don't need help with
this).
The reason I want this is I don't want ownerid2, ownerid3, ownerid4 taking up loads of space in separate columns in the database.

You could try this:
SELECT * FROM table_name WHERE FIND_IN_SET(?, Owners);
where ? has to be replaced with the user id, for example
SELECT * FROM table_name WHERE FIND_IN_SET(6, Owners);
as other pointed out, you might also want to read about database normalization, and here is the docs for FIND_IN_SET

A cleaner way to accomplish this is to use a separate table for the owner relationship.
Table = Owner, has ID.
Table = Test, what you are currently trying to get
Table = OwnerTestRel, has a foreign key to Owner and one to Test.
Then your query can become something like
Select test.*
FROM test
JOIN OwnerTestRel ON test.id=OwnerTestRel.testId
WHERE OwnerTestRel.ownerId = {owner's Id}

could this possibly be what you're after?
SELECT * FROM table_name WHERE Owners LIKE '%6%';
????
EDIT: doing it this way would pull up other owners like 68, 600, 6123. you should look into database normalization.
EDIT2:
just thought of this, since there's no spaces between your commas you could use the explode(); function like so:
$ownerid = //WHATEVER THE OWNERS ID IS
$sql = "SELECT * FROM table_name WHERE Owners LIKE '%$ownerid%'";
$result = mysqli_query($con, $sql);
while($data = mysqli_fetch_array($result)){
$owners = $data['Owners'];
$owners = explode(',',$data);
if(in_array($ownerid, $owners){
//SHOW ROW DATA HERE
}
}

Related

Random like order with mysql and PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I use PHP and Mysql. I have a table that looks kind of like this:
id title
----------
1 my title
2 another title
3 The last title
Now I want to select them with a random like order.
I will need to use LIMIT because of the query size.
The random like order should always be the same random order every time.
Example result, every time
3 The last title
1 my title
2 another title
Do another query run:
3 The last title
1 my title
2 another title
The same random like result appear.
Possible solutions
Add a real random number stored as a new column generated by insert.
Some fancy SELECT query that does some magic.
Something else?
Why I want this is that I insert products, first from one site, then from another. In the result I want to present them as a mix.
It's not really random at all, but then again your request wouldn't work with random numbers. You want some sort of a hash of each record to use in the order by. You can probably find something better, but as a simple example you could use:
SELECT * FROM my_table ORDER BY MOD(id, 2);
Which you can see working here:
http://sqlfiddle.com/#!9/269a4/1/0
Use ORDER BY RAND().So your query should be :
select * from <tablename> where <cond> then ORDER BY RAND() limit <startlimit>, <end limit>

How can I Select duplicate results, insert the results to a new table and delete them from the original table? (phpmyadmin) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have 2 tables, both with the same column names that look like this
I have an app that is used for clocking in/out. A user goes up to a tablet and selects there name, the data is then inputted into table 'clocktable' as shown above.
What I need to happen is when a user presses their name for a second time (to clock out) it detects that there is already a value in the table with the same employeename value, and moves both clocks to a different table 'oldclocks' so that a record is kept of clocking in/out times. The reason it needs to be in a separate table is I have a web page that displays the 'clocktable' table so we can determine who is in the building.
How can I go about doing this?
I can execute
SELECT DISTINCT EmployeeName, time FROM clocktable ORDER BY EmployeeName
from within myphp and then execute
INSERT INTO oldclocks SELECT Employeename, Department, time FROM clocktable;
Afterwards, but I cannot seem to execute them both at the same time in order to get them into a php script. Also is there a way I can delete the select results from the 'clocktable' at the same time?
Help with this would be greatly appreciated.
You can add an indicator, where 1 is in and 0 is out & if you want to save the logs, then creat another table to INSERT a record whenever the indicator's value change.
1- add 2 new attributes in table A, lets say userid & in_out for an example.
2- in_out = 1/0 where 1 is in and 0 is out
3- creat table B and insert userid & time every time the in_out value change.
And that's how you know if the user in the building or out, also you will keep logs of every move
2 tables are not nessesary. And it is bad idea.
Why dont you simply add boolean IN_BUILDING which will have 0 ->Not in building and 1->In building. Then you can do:
SELECT EmployeeName FROM clocktable WHERE IN_BUILDING = 0; which will show you all employees who are not in building
or
SELECT EmployeeName FROM clocktable WHERE IN_BUILDING = 1; which will show you all employees who are in building that moment.
Its easier also because you can change it value easy on click.

"likes for comments" database designing? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
i have database tables for Posts and comments.
i want to allow users to put a like or dislike for each comments and posts.
so..i have few ideas of doing this. please tell me either i am correct or not.
create two additional columns in comments table.
likes | liked_uids
if a person clicks on like button then add +1 for the current value in likes field, else add -1 for current value.
and add user's id to liked_uids field as a sting separated by "-" dashes.
then next time i can get that string to an array and check that,
either current user id has recorded or not. if user id is, then can decide that user have participated for liking.
but i have little problem on this structure, that what will if more than one user going to like at once for a post ? then i may lose some data from liked_uids string (one last uid).
so please tell me what is the correct way of doing this?
You can create like this->
id type ('comment/like') uid comment post_id
1 comment 1 good post 100
2 like 2 null 101
3 like 1 null 102
4 comment 3 bad post 104
It is not recommended to store like count.If you want to count the likes for a particular post:
select count(*) from tableName where post_id = 100
Storing user id separted by any delimiter will land you on problems, Hence not recommended. It will be tidies job to update or retrieve if your store user id using delimiter.
If you want to see if particular user is liked a particular post or not, use below query:
select count(*) from tableName where post_id = 100 AND uid =1
One way is to use a separate Likes table with columns Likes, DisLikes, Likes_UID, DisLikes_UID and mapping table for comments and likes ex: Comments_Likes and posts and likes Posts_Likes
I'm typing way too slow on my mobile ^^ Everything already answered.
I never did anything that is similar, but I wouldn't add the two columns in the comment table. I would rather create a new table like "votes" and it would have following columns.
comment_ref | like | user_ref
Every time someone likes a comment you insert a new line there. You could also make the combination comment_ref and user_ref as a key, so you can't insert it twice.
In the end you would just make a query as such to get the votes of a single comment.
SELECT COUNT(*) FROM votes WHERE comment_ref = 123

PHP - How To Compare Two Columns? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having a problem in solving this quiz program. I am currently stuck at inserting a row. I'll explain it in detail: Let's say I have two tables, named "Table 1" and "Table 2". Table 1 has all the questions and correct answers which were inputted by the teacher. Table 2 contains answers given by students. How do I "compare" data from the 2 tables then insert the "result" of the student's answers, whether they're "correct" or "wrong", to Table 2? The image below is the kind of table I am trying to achieve.
I've been at it for almost a day and I've come to the conclusion that I am stuck.
May you provide me some ideas, concepts, or even sample codes?
tl;dr: Compare table1.correct_answer to table2.student_answer then provide data for table2.result
Considering that the two tables have id columns and the questions and answers are in the same order (with same id). You could do something like this:
First put all the corrects answers into an array.
$result1 = mysql_query("Select * From table1");
while ($correct_row = mysql_fetch_array($result1) ){
$correct[ $correct_row[id] ] = $correct_row[correct_answer];
}
Then compare them with the students answers and update the table of the result while going thru every answer of the student.
$result2 = mysql_query("Select * From table2");
while ($student_row = mysql_fetch_array($result2) ){
if ($student_row[student_answer] == $correct[ $student_row[id] ] ){
mysql_query("UPDATE table2 SET result=correct WHERE id=$student_row[id]");
} else {
mysql_query("UPDATE table2 SET result=wrong WHERE id=$student_row[id]");
}
}
Hope that helps.
Table1 should have some kind of ID. Table2 needs a column with references Table1s ID as a foreign key.

mysql liking you and others [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wonder how to do this
I mean the "You and 19,389 others" How do you identify YOU from others. If a user is logged in and like a status or favorite a status or something like facebook. I wanna catch the "you" part. But I don't seemed to understand the logic behind it.
I'm using php, and jquery. How do you sort this out in sql? or is it really sql? how do you define the user from others?
If I'm doing it the wrong way or asking it the wrong way, please tell me the right way and the answer guys I badly need your help.
With only one query you can do like this:
SELECT COUNT(user_id) AS everybody, SUM(user_id = "your_user_id") AS you FROM fb_likes WHERE post_id = 4;
Then if you is bigger than zero print (in fact it must be 0 or 1 only):
You and [everybody - you] others like this.
Else
[everybody] like this.
First of all, when you click on "Like" there's an entry going inside a database assigned to the post. So let's say the post has ID 1234567890 so you going to see something like that inside your database table :
PostID UserID
1234567890 54543534
1234567890 75231415
1234567890 78653421
1234567890 99653221
// Query example
SELECT COUNT(*) FROM LikeTable WHERE PostID = 1234567890
// Return 4
First of all you count all like, there's 4 here.
After, you check if the UserID of the current logged user is in the database.
Yes. He liked.
No, he didn't liked.
Let's say we are user 54543534. I am in the database so I liked the post.
// Query example
SELECT PostID FROM LikeTable WHERE PostID = 1234567890 AND UserID = 54543534 LIMIT 1
// Return 1
Let's say we are user 8748977777. I am NOT in the database so I didn't liked the post.
// Query example
SELECT PostID FROM LikeTable WHERE PostID = 1234567890 AND UserID = 8748977777 LIMIT 1
// Return 0
So I say the IF/ELSE statement would be :
IF I liked
You and COUNT - 1 others like this.
ELSE
COUNT like this. Click to like.
EDIT :
I think something like that will make the job :
SELECT COUNT(PostID) AS All, SUM(UserID = X) AS Liked FROM LikedTable WHERE PostID = X
Query for others:
SELECT COUNT(userid) FROM likeTable WHERE postid = $postid
Query for you:
SELECT COUNT(userid) FROM likeTable WHERE postid = $postid AND userid = $_SESSION['userid']
Then do:
if ($youCount == 1) {
echo "Liked by you and " . $othersCount - 1 . " others";
}
else {
echo "Liked by $othersCount people";
}
I don't know much about optimal database schemas, but the way I see this is that every single like in Facebook terms is a node on FB's social object graph. Which probably means that every single like is an individual database record on some table. To count the number of likes, you just do a COUNT on like records associated with a post.
Now if that's the case, then it's trivial to include some identifying information on the like record on who performed it. Top-of-head, it would be an FK on a user's PK.
So if you're logged in on Facebook, it knows who you are and what your associated user ID is. For every post that it aggregates the likes to, it can counter-check your user ID to the user ID FKs on the likes, and determine which of those posts you've actually liked. Hence, it can conditionally display either You and 14,000 others like this or 14,001 like this.
Struggling to understand the question but I think you might be trying to do too much in one go.
Either Run two queries, one to check if 'you' (your session id etc.) are in the table that records this or pull the whole lot and use in_array or something similar to see if 'you' are in the list.

Categories