Show MySQL values in specific order [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
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.

Related

I am trying to run a query in php its working in phpMyAdmin but not in script [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Iam trying to fetch related articles for a blog based project
Query :
$query = mysqli_query($con, "SELECT * FROM posts WHERE cat_id like '%$cat_arr[i]%' AND NOT post_id = '$postid'");
Problem :
When ran in phpMyAdmin as a normal query it is fetching 3-4 rows as required but in scripts, it is fetching all rows in the posts table
It seems that the variable '%$cat_arr[i]% is blank.
So if you are running the query:
$query = mysqli_query($con, "SELECT * FROM posts WHERE cat_id like '%$cat_arr[i]%' AND NOT post_id = '$postid'");
Instead of two conditions, only one condition post_id = '$postid' is getting into consideration.
If we pass blank in LIKE %%, it is equal to not adding the conditon.
So, best solution is to evaluate the variable first:
if (isset($cat_arr[i]) && ! empty($cat_arr[i]) && ! empty($postid)) {
$query = mysqli_query($con, "SELECT * FROM posts WHERE cat_id like '%$cat_arr[i]%' AND NOT post_id = '$postid'");
}
else {
// Another condition
}

How to count certain rows in a MySQL table? [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 currently have a table that looks like this.
http://i.stack.imgur.com/KFP6Q.png
This is a comment system. the column id gives the comment an ID. the server_id is the ID for the section the comment was posted on. The user_id is the ID for the person who posted it. And lastly, the comment is the comment itself. Here is how I created the comment:
http://pastebin.com/VHUDW6Dm
What I want to do is create a variable, $commentcount, that will count how many comments there are for a server and be able to display them on a page. If someone could direct me to a function that can help me with this or actually create the code here, it would be greatly appreciated.
Since you want the number of comments per server, you can use the SQL GROUP BY clause to aggregate the resulting rows by the unique server_id.
SELECT server_id, COUNT(id) FROM comments GROUP BY server_id;
This will return the count for each server_id group. If you are only displaying this for a single server_id at a time, you can simply use
SELECT COUNT(id) FROM comments WHERE server_id = <your server id>;
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
A very rough draft for you would be to iterate a count.
$q = mysql_query("MYSQL QUERY TO GET ALL THE COMMENTS FOR THE USER");
$count = 1; // Start the count, preferrably at 1
while ($comment = mysql_fetch_assoc($q)) {
$count++; //iterate the count
}
echo $count; // Echo's the count;
use following mysql query
select count(*) as count_comment from comments;

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";

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'

SQL query display last row by a column [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 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.';

Categories