This question already has answers here:
PHP PDO retrieves duplicate data
(2 answers)
Closed 1 year ago.
I'm newbie in PHP and when I use PHP to query select, I got 2 values, I realize it may be make slow if have 2 duplicated values.
So I want to ask about it and I also want to get just 1 values from select
thanks for reading my question.
here is picture:
It dipends on what your i guess sql query respondes to your query. However if you want cut down your result in sql you can use LIMIT 1
SELECT ...
FROM ...
WHERE ...
LIMIT 1
This question already has answers here:
When to use VARCHAR and DATE/DATETIME
(5 answers)
Closed 2 years ago.
I have a table that holds few records less than 20. The table (cow_inactive) is just with ID (int) & COW_INACTIVE_DATE (varchar) columns. I am trying to use the DATEDIFF to get all cows that are ready for insemination after 60 Days of being inactive.
SELECT * from cow_inactive where DATEDIFF(CURRENT_DATE, inactive_date)>=60
Result :
Showing rows 0 - 0 (1 total, Query took 0.0022 seconds.)
you need to update field data type COW_INACTIVE_DATE (DATETIME). then run above query
This question already has answers here:
Simple PHP Pagination script [closed]
(3 answers)
Closed 6 years ago.
A mysql table contain 100 rows. I am trying to display first 20 rows with 5 rows per page.
I think query should be like this
SELECT * FROM `table` top 20 LIMIT 0, 5
but how can i use this concept. waiting for your help.
You can use LIMIT and OFFSET controls, LIMIT is for max rows you want to show and OFFSET is the starting position:
SELECT * FROM db_name ORDER BY db_table LIMIT 5 OFFSET 0
This question already has answers here:
Returning query results in predefined order
(11 answers)
Closed 6 years ago.
I have a IN clause query in which I pass this ID'S (10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)
My query is,
SELECTpost_jobs.idFROMpost_jobsWHEREidIN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)
It execute correctly and returns result first id likes 1 then 4 then 7 and so on...that is ascending order
My Need is to get first result of id 10 then 19 then 23 as the order of ID that I passed is there any way to get such type of result in output.
Try this
SELECT post_jobs.id FROM post_jobs WHERE id
IN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29) ORDER BY
FIELD(id,10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29);
In Codeigniter
$this->db->_protect_identifiers = FALSE;
$this->db->select('id');
$this->db->where_in('id',array(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29));
$this->db->order_by('FIELD(id, 10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)');
$this->db->get('post_jobs');
If you are using codeigniter 3 then write or remove the line of protect_identifiers
$this->db->protect_identifiers(False);
Try this one
SELECT post_jobs.id FROM post_jobs WHERE post_jobs.id
IN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29) ORDER BY
FIND_IN_SET(post_jobs.id,10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29);
This question already has answers here:
Read one column from one row from a MySQL database
(8 answers)
Closed 8 years ago.
I am having an issue declaring a PHP variable as a MySQL result.
Code:
$num = mysql_query("SELECT num FROM numbers ORDER BY num DESC LIMIT 1");
What it does it retrieve the latest entry in the database, which is an int, and I want it to store into the PHP variable $num. Is this possible?
Is this possible?
Yes, that is possible.
You should, however, use mysqli or PDO instead of the old mysql_ functions.
See: Why shouldn't I use mysql_* functions in PHP?
An example of how it could look:
$result = $mysqli->query("SELECT num FROM numbers ORDER BY num DESC LIMIT 1");
$num = $result->fetch_object()->num;