I'm currently trying to retrieve values from a database.
New Users are given a UNIQUE ID once they register on my website:
Keys are stored into my database with a link such as:
website.com/file.php?id=4&key=123abcd123
What I was planning on having was the website pulling the keys ONLY related to the ID of the user.
Lets say User 4 inputs a key into the database, I want to have a universal .php file that will retrieve HIS codes (codes with the ID of 4) ONLY.
I'm sorry if this doesn't really make sense, English isn't my first language.
For finding particular record in mysql query pass the id value in the place of 4 below.
SELECT * FROM tablename WHERE UserID = '4'
if you want to get particular value then use like this
$query = "SELECT * FROM tablename WHERE UserID = '".$_SESSION['ID']."'";
Refference
If you use SQL, it is easy to search for all keys for a user:
select cdkey
from YourTable
where UserID = 1
Note that anyone can edit a URL. So unless you take additional measures, any user could ask for the cdkeys of any other user, just by modifying the id parameter in the URL.
Related
I have a database where I'm getting a random row from a table depending on a few variables. I'm trying to make it so it adds their username in this format (Admin, Jerry, Ben) once they view the advertisement (My project - clients view ads for Bitcoin). I want to make it so that once they view it , it adds their name in to a row called users_viewed_add (example is above). I need it to only get random rows based on if their username does not exist in the row users_viewed_add atoll so they can't view the ad more than once.
At the moment, I'm trying to do:
$query_second = mysqli_query($con, "SELECT * FROM business_advertisments WHERE ($users_credits >= amount_per_click) AND (users_viewed_add) NOT LIKE '$username' AND users_name != '$username' ORDER BY RAND() LIMIT 1");
As you can see, the AND (users_viewed_add) NOT LIKE '$username' is not working for me as it's reading the text as a whole word. The problem is that I'm using comma's to seperate the usernames of the clients who have viewed the ad. Is there any work around for this ? I know the method to check a block of text but it wouldn't work in a SQL statement I'm pretty sure.
You can do:
AND CONCAT(',', users_viewed_add, ',') NOT LIKE '%,$username,%'
But you probably want to make a separated many to many table, and use user id instead of user name.
If you want to use multiple values in your LIKE comparison, you can use:
AND users_viewed_add NOT LIKE ALL ('$username')
This will check that none of the $username values exists in the users_viewed_add field. You may need to format your variable properly so the SQL works.
I found the answer from this post: How do I add multiple "NOT LIKE '%?%' in the WHERE clause of sqlite3 in python code?
The correct SQL syntax to check a word doesn't exist in a row is:
AND users_viewed_add NOT REGEXP '[$username]'
I want a straightforward answer with an example (NO LINKS please) to see how I can retrieve a specific person's last name from a table in a MySQL database using PHP and save it into a variable (i.e. $_Session). I have been looking for this question and I don't get anything related to one row in PHP. If this can be done better with mysqli* functions then I would be glad to see it.
Example
$getData = "SELECT lastname FROM person WHERE name='$name'";
$getData_q = mysql_query($getData) or die('Error');
Then i want something like this:
$_SESSION['lastame']=$getData_q; <- Please correct me,
I'm not asking to get a lastname from an input, I'm asking to get it from the table person in the database with the SELECT.
Use
$getData_q = mysqli_fetch_object($getData_q);
$_SESSION['lastame']=$getData_q->lastame;
But ensure that that num rows is greater than 0 and data would return a single row.
I want to be able to bring back the earliest logon time per user, so only 1 record (the earliest record) displays for each user
I've tried various ways of GROUP BY but can't seem to get it quite right (if that is actually the correct way of doing). username is the unique value which can be used to GROUP BY
Here's the code I'm currently working with..
SELECT username, name, logon, added FROM data WHERE (date(added) LIKE '$date') AND (logon = (SELECT MIN(logon) FROM data))
I've also tried (below) but only get one result back, only displaying one user
WHERE (date(added) LIKE '$date') AND logon = (SELECT MIN(logon) FROM data)
The first image is what I'm currently getting, the second image is how I want my results to display, please see below
Let me know if you require anymore information, I've tried to put as much as possible
Thanks,
Tom
You are close. Your query needs a correlation clause:
SELECT d.*
FROM data d
WHERE date(d.added) = '$date' AND
d.logon = (SELECT MIN(d2.logon) FROM data d2 WHERE d2.name = d.name);
Note: The logic for added is confusing. First, you should not use like with dates. And, for that matter, you should not be inserting parameter values into the string, you should be using query parameters. And, actually, I don't see the need for that column; your question doesn't mention added.
use this query , problem will be solved
SELECT username, min(logontime) FROM data where date(logontime) = '2016-08-10' group by username
do this:
<?php
$users = array();
$sql = "select * from users";
$result= mysqli_query($conn,$sql);//$conn is your connection object
while($user = mysqli_fetch_assoc($result)){
if(!in_array($user['name'],$users)){
$users[$user['name']]=$user['logOnTime'];//replace key names with your table row names
}
}
//$users is an array that has your user names as key and their last logon time as value
?>
I am working on a project where I need to maintain user specific content.
One user can view one content only one time in 24 hours.
After 24 hours it will be made available to the same user again.
So when a user successfully views a content I store the username of the user in the database in a comma separated value.
For example: when the user user1 successfully views a content his username will stored in user_statusas user1 along with other users likes user1,user2,user3,.......,etc.
Now what I want is to check the content if the username user1 exists in the database or not.
If it exists then content will not be made available to him for next 24 hours.
But for some reasons I cannot check a particular user from the group of comma separated usernames using if...else.
I can do it with if...else and explode but that does not satisfies my need. I want to know that can I check for one username in the WHERE clause of SELECT statement?
For ex. something like SELECT * FROM user_data WHERE user_status != '".$something."'.
Is it possible by using explode first then checking it in WHERE clause or just by some other method? If yes, then how? Please help me php experts.
Is it possible something like this?
$uname = $_SESSION['username'];//username of the logged in user
$ths = "SELECT * FROM user_data";
$its = mysql_query($ths) or die(mysql_error());
$uhs = mysql_fetch_array($its);
$user_status = explode(',', $uhs['user_status']);// first explode the comma separated usernames
$ths = "SELECT * FROM user_data WHERE user_status != '".$uname."'";// then check if the logged in username does not exists in the database
$its = mysql_query($ths) or die(mysql_error());
Can the exploded list of usernames be checked in the WHERE clause? Is it possible? Or is there any method to do this? If yes then how?
I think you are looking at it all wrong. That is a lot of maintenance for a very simple task.
You could just have another table, where you keep user id & data id pairs along side with a timestamp.
For example, for user data 1 and user 1 you will save:
uid | did | ts
--------------------
1 | 1 | TIMESTAMP
To check if a user watched an item, just select from that table using the user id and data id.
If no row is found, or the the current timestamp minus the row timestamp is more than 24 hours, allow the user to view the data, otherwise - don't allow it.
You may use LIKE to search some string for existence of another string:
SELECT * FROM `user_data` WHERE `user_status` LIKE '%user1%';
See: https://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html
I'm trying to do a customizable and extendable profile system for my CMS. From the user perspective it is straight forward, but for the admin I want all data, including the profile data, to be searchable. Profile fields may be added by "plugins", which may also add new fields to search on. I don't know if what I'm trying to do with MySQL to make this work is possible or if I'm going at it completely the wrong way.
So I have the users stored in one table (users), with columns for id, email, password and access_level.
I then have another table with profile information (profiles), stored as user_id, parameter and value. The parameter could eventually be put into a separate table again (so it isn't repeating itself), but for now I'll leave it like this.
The parameter and value are basically the profile data. For example, parameter may be "age" and the value may be "22".
What I want to try and do, is select the users table, with the profile information joined so the parameter is mapped to an additional column. So it ends up like so, straight from MySQL:
id email password access_level age
1 a#a.com ***** 1 22
2 b#b.com ***** 2 25
3 c#c.com ***** 2 25
I've been looking at pivot tables all afternoon, but from all I can see the "column name" is pre-defined. In this case I want the "column name" to come from the row itself.
If it isn't possible to do it with a single query, what other methods are there? I'm using PHP if the best method is to do it via that.
Any suggestions would be much appreciated. :)
Well, if you need to know the column names in advance, you can query the information_schema database:
SELECT COLUMN_NAME
FROM information_schema.COLUMNS
WHERE TABLE_NAME='your table'
However, that gets the raw column names. If you're aliasing in your query, you'll have to fetch them indirectly:
SELECT somefield AS alias1, otherfield AS alias2
FROM ...
and then
$stmt = mysql_query($query);
$first = true;
while($row = mysql_fetch_assoc($stmt)) {
if ($first) {
$column_names = array_keys($row);
... display column names here
$first = false;
}
... output row here
}