SQL query display last row by a column [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I trying to display all information submitted by a specific username(variable) in my case:
<?php
$Username = $_SESSION['VALID_USER_ID'];
$q = mysql_query("SELECT * FROM `article_table` ORDER BY FIELD (Username, '.$Username.') DESC LIMIT 1");
while($db = mysql_fetch_array($q)) { ?>
Your Articles: <?=$db['Subject'];?><br />
<? } ?>
But don't work, I'll display the last article subject(for example) from the table and not from the specific username, where I'm wrong?

That is not how ORDER BY works. You first need to filter the results by using the WHERE clause.
$q = mysql_query("SELECT * FROM `article_table` WHERE `Username` = '$Username' ORDER BY `other_column` DESC LIMIT 1");
Replace other_colum in this query by the name of your date column: ie. date_created

Here is your query:
SELECT *
FROM `article_table`
ORDER BY FIELD (Username, '.$Username.') DESC
LIMIT 1;
This should be returning the username you are looking for when it is available. The logic is that field returns 1 when the name matches and 0 otherwise. So the descending keyword puts the match first. If there is no match, then you will get another row.
Presumably the data does not exist. You can test that easily enough by running:
SELECT *
FROM `article_table`
WHERE Username = '.$Username.';

Related

using ORDER BY id DESC and where [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
how can i use right code for using ORDER BY id DESC and WHERE
$sel = "SELECT * FROM items where portfolio_id=".$_GET['folio_id']."ORDER BY id DESC";
Add a space here as shown.
$sel = "SELECT * FROM items where portfolio_id=".$_GET['folio_id']." ORDER BY id DESC";
----^
Also, don't pass your parameters like $_GET or $_POST directly into the SQL query as it will definitely lead to SQL Injection Attacks. Filter those parameters or make use of Prepared Statements.
add a space before 'ORDER' at least
$sel = "SELECT * FROM items where portfolio_id=".$_GET['folio_id']." ORDER BY id DESC";
You need provide space before ORDER BY
$sel = "SELECT * FROM items where portfolio_id=".$_GET['folio_id']." ORDER BY id DESC";
Simply Use this :
$sel = "SELECT * FROM items where portfolio_id ='$_GET[folio_id]' ORDER BY id DESC";

mysql query for password should not be last three passwords? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have pwd_history table containing columns id(PK),old_password, created_date,user_id. When ever user modify the password an extra row will be added to that table.I want a single query through which it should verify the entered password is in last three passwords modified by the user. Is there a way in Mysql? thanks.
If the password was hashed with mysql's default password, then
try this, with valid input values:
Updated:
select * from (
select * from old_pwds_table
where user_id=input_id
order by created_date desc
limit 3
) a
where
old_password=password('new password')
Former answer:
select * from old_pwds_table
where user_id=input_id
and old_password=password('new password')
order by created_date desc
limit 3
Check this function hope It helps to you:
function check_old_pass($new_pwd){
$result=mysql_num_rows(mysql_query("select * from old_pwds_table
where user_id=input_id
and old_password=$new_pwd
order by created_date desc
limit 0,3"));
if ($result==1) {
return true;
} else {
return false;
}
}
SELECT
*
FROM
pwd_history
WHERE
user_id = 'USER_ID'
AND
oldpasswords = 'NEW_PASSWORD'
ORDER BY
created_date DESC
LIMIT 3

In mysql, how to I query to see if a specific user is in a table? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table of users and I want a query that will tell me if a particular user is listed in the table. For example, in the table below:
Users | Ages
Bob | 22
Mike | 30
Sue | 21
Can someone help me with a query that does something like,
If (In USERS_TABLE, Mike is in column 'Users'){$userPresent="true";}
Mysql Num rows can help you with that
$query = mysql_query("select * from users_table where Users = 'Mike'");
if(mysql_num_rows($query))
{
echo "user present";//use your code how you want
}
else
{
echo "user not present";
}
mysql_ extension is deprecated as of PHP 5.5.0.
So Please choose PDO or MYSQLI api for better experience.
Your query would be:
SELECT * FROM USERS_TABLE WHERE Users='Mike'
You could run a query like
SELECT * FROM USERS_TABLE WHERE Users='Mike'
Then, you could check if the number of rows returned from that query was more than 0.
SELECT count(*) as count FROM USERS_TABLE WHERE Users='Mike'
If count>0 then user exists.
or if you want that user record,just use as below
SELECT * FROM USERS_TABLE WHERE Users='Mike'
Select * FROM USERS_TABLE WHERE Users = 'mike', here you can choose specific table like
Select Users FROM USERS_TABLE WHERE Users= 'mike'

Show MySQL values in specific order [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I have the following piece of code.
$sql = "SELECT TitreEvent, DescriptionEvent, MomentEvent, image_small, Confidentialite, ID, Creation, Username
FROM events_home
WHERE ID = '" . $dnn['IDcontact'] . "'
ORDER BY Creation DESC";
My problem is that I want to display the most recent events first (regardless of who the user is). But now, the events are displayed according to users (IDcontact), and new events are placed at the beginning of the portion of the user.
So if a user is like the second one in the table, the new events he will add will be displayed after the events of the first user. But I want to see the newest events at the top of the list, and I have no idea how.
EDIT So there is the missing part. I have tried to put SORT BY RAND () in the first query, but it dosen't change anyting. It just place the first user at the second place, and the second one in the first place.
<?php
$dn = mysql_query("SELECT IDcontact FROM contacts WHERE ID = '".$_SESSION['id']."'");
if(mysql_num_rows($dn)>0)
{
while ($dnn = mysql_fetch_array($dn)) {
$req = mysql_query("select TitreEvent, DescriptionEvent, MomentEvent, image_small, Confidentialite, ID, Creation, Username from events_home where ID = '".$dnn['IDcontact']."' ORDER BY Creation DESC");
if(mysql_num_rows($req)>0)
{
while($dnn = mysql_fetch_array($req)) {
?>
I suppose you're running this in a loop from a previous query result. In which case you need to change the 'parent' query. If there's no such thing then why are you sorting results by the user's ID and expecting all results regardless of the user?
EDIT:
You should never run queries in loops.
<?php
$dn = mysql_query("
SELECT e.TitreEvent, e.DescriptionEvent, e.MomentEvent, e.image_small, e.Confidentialite, e.ID, e.Creation, e.Username,c.IDcontact
FROM events_home e LEFT JOIN contacts c ON c.ID = e.ID
ORDER BY e.Creation DESC
LIMIT 0,30"); // Just in case
Here's your query, hope that helps.

MYSQL + PHP issue where Firstname [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Guys im having a issue with my mysql php problem ok i want this to pull table information out of my mysql database in it shows up the firstname by the number 10 but i want to add more too it like 10 100 1000 in other numbers how can i do this from this code
$result = mysqli_query($con,"SELECT * FROM tvshows
WHERE FirstName='10',100,1000");
SELECT * FROM tvshows WHERE FirstName IN (10,100,1000);
You can look here:
http://dev.mysql.com/doc/refman/5.0/en/any-in-some-subqueries.html
and here:
http://dev.mysql.com/tech-resources/articles/subqueries_part_1.html
If you want to select strings, you need to '', ex.:
SELECT * FROM tvshows WHERE FirstName IN ('10','100','1000');
If Numbers like this:
SELECT * FROM tvshows WHERE FirstName IN (10,100,1000);
The IN operator allows you to specify multiple values in a WHERE clause.
$result = mysqli_query($con,"SELECT * FROM tvshows
WHERE FirstName IN('10','100','1000')");
See Example
You have to change the = for IN
$result = mysqli_query($con,"SELECT * FROM tvshows WHERE FirstName IN(10,100,1000);
SQL IN clause documentation

Categories