php sql error (from,order,where position) [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
i made this:
$query1=mysql_query("select id, testo, titolo from events order by DESC WHERE id < 225 LIMIT 1");
while($query2=mysql_fetch_array($query1))
{
echo $query2['titolo'];
}
But doesn't work, i don't understand why, someone can help me?
I think the problem is in the query.
UPDATE: I made two mistakes:
The first: i have missing the column name in order by clause.
The second: The position of ORDER

Write your query as below:-
SELECT id, testo, titolo FROM events WHERE id < 225 ORDER BY id DESC LIMIT 1
As suggested by chris85, For understanding select ...from... where... order...limit,
This link is very useful.
Hope this will help you :)

You are using ORDER BY before WHERE Clause you can not get the result like this way.
Second issue is that you didn't mentioned the id column or any other column in ORDER BY.
Modified Query:
SELECT id, testo, titolo FROM events WHERE id < 225
ORDER BY id DESC
LIMIT 1
Side note:
Please use mysqli_* or PDO instead of mysql_* becuase this extension deprecated and not available in PHP 7.

you have at least 2 errors. Rewrite your query to look like:
SELECT id, testo, titolo FROM events WHERE id < 225 ORDER BY id DESC LIMIT 1

You're missing the column name in your order by clause. That section should be something like order by col_name desc. Also the where clause comes before the order by as described in above answers.

Related

how to make this mysql query fast? I have millions of data inside my table [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 a table called ad_view and it has 18.9 million number of data inside that table.
This is the table design of ad_vew
ad_view
-------
ad_id
network_id
publisher_id
landingpage_id
advertiser_id
I am using this query to get the ad_id and the count of ad_id depending on the network_id, so in this case the network_id is 4.
select ad_id,count(ad_id) as strength
from ad_view
where network_id = 4
group by ad_id
order by strength desc
limit 10
This query takes forever to load. Do you have any suggestion how to make this query fast?
I am not a database expert, if it takes to redesign this table, I would do that.
Any suggestion will be greatly appreciated, thanks!
This will help:
ALTER TABLE ad_view ADD INDEX (network_id, ad_id).
Make sure you have tuned your innodb_buffer_pool_size to hold the frequently-requested portion of your table in memory.
You might like my presentation, How to Design Indexes, Really. That presentation doesn't go into indexing for GROUP BY, but you can think of it like a range condition.
1 Create an index on network_id since you're searching by it
ALTER TABLE `ad_view` ADD INDEX (`network_id`);
2 If you're trying to get the count of ad_id for a given network_id, why do you need ad_id in your results? and why do you need to order by? I don't understand this. If all you want is how many ad_ids for network_id 4, then do:
SELECT COUNT(IF(network_id=4,1,null)) as strength from ad_view
It will return just a number. See this demo
PS: your initial post included a broken query (in the order by clause), which you changed after I made a comment. Your current query still doesn't give you what you say you want. I just tried it on this fiddle

PHP MySQL - Start from ID x [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'm making a newssystem, the last ID (so the last news) must be at the top, and now I want that below the first news box (not in the same box as the last news) are coming the other news, so that the last ID is not included. I've no ideas.
Last news code:
<?php $last_news = mysql_query("SELECT FROM news ORDER BY id DESC LIMIT 1"); ?>
Other news, below the last news: (?)
<?php $other_news = mysql_query("SELECT FROM news ...?"); ?>
Try to use limitwith the offset 2
<?php $other_news = mysql_query("SELECT FROM news order by id desc limit 2,numerOfNewsYouWillShow"); ?>
This will not show you the last news. but the news before. Hope I have understand your issue correct.
Please do not use mysql_* functions. The mysql_ API is deprecated.
Another way to do this is in stead of retrieving the data from the database in 2 queries is to retrieve the data once and then split it in php.
$last_news = array_shift($allnews);
Then $allnews will contain all the other news and $last_news the latest news article. This will also mean you only hit your db once which is better for scaling.
Please do not used the mysql_* functions. They are depreciated because of security issues. Use PDO or MYSQLi in stead
More info on shift function: http://php.net/manual/en/function.array-shift.php
below the last news, you can use
ORDER BY id DESC LIMIT 1,1
id field
23 last news //ORDER BY id DESC LIMIT 1
19 yesterday //ORDER BY id DESC LIMIT 1,1
18 old
for one query
order by id desc limit 2

retrieve last 2 rows from a table in mysql database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am working a website.And i want this query for a particular functionality.I have tried it but it is not working.
how to retrieve last 2 rows from a table in MySQL??
I want a query in MySQL.
thanks in advance.
SELECT users.* FROM users ORDER BY users.id DESC LIMIT 2
select id, type, details from supportContacts ORDER BY id DESC LIMIT 2
ORDER BY id DESC LIMIT 2 helps you retrieve last two rows
Whats your table structure?
is there any date time field to get last records?
Is there any primary key?
If primary key then you could use:
Select * from table_name order by primary_key_column desc limit 2;

Show a random mysql row, but not the same twice [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 8 years ago.
Improve this question
I'm trying to create a database that holds all my information with questions, users, answers etc.
I've been thinking all day how to solve this problem of mine. Okey, so the problem is like this:
Let's say I have a table in my DB, with questions (300++) and the user that logs on will get a random question shown. But never the same question again, so I'll need to store this information in a separate table with the user-ID and the question-ID. And I'll need to create another table that stores the answers to the questions.
So how would this PHP/MYSQL-code look like? Because I'll need to find a random question that hasnt been shown to the same user again.
If something is unclear, please let me know. And thanks in advance
You can use RAND() function and NOT IN
Example:
SELECT * FROM `questions` WHERE id NOT IN (5, 3) ORDER BY RAND() LIMIT 1;
You have to fetch question id using sub query
Example
SELECT * FROM `questions` WHERE id NOT IN (SELECT question_id FROM shown_questions WHERE user_id=1) ORDER BY RAND() LIMIT 1;
You can use an outer join to select items that have not been used already. It might be a bit faster than NOT IN if you set up the indexes:
SELECT questions.* FROM questions
LEFT JOIN shown_questions
ON (questions.id=show_questions.question AND user_id=42)
WHERE shown_questions.question IS NULL
ORDER BY RAND() LIMIT 1;

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;

Categories