this is my script
<?php
if(isset($_GET['id']))
{
$id = $_GET['id'];
}
else
{
$id = 1;
}
$random = $conn->query("SELECT * FROM records ORDER BY RAND()");
$row = $random->fetch();
$stmt_1 = $conn->prepare("SELECT * FROM records WHERE id > ? ORDER BY id ASC LIMIT 1");
$stmt_1->bindValue(1,$id);
$stmt_1->execute();
$stmt_1->setFetchMode(PDO::FETCH_ASSOC);
$row = $stmt_1->fetch();
$id = $row['id'];
$stmt_1 = $conn->prepare("SELECT * FROM records WHERE id < ? ORDER BY id DSC LIMIT 1");
$stmt_1->bindValue(1,$id);
$stmt_1->execute();
$stmt_1->setFetchMode(PDO::FETCH_ASSOC);
$row = $stmt_1->fetch();
$id = $row['id'];
?>
But I have a problem with it. For example i have 4 records in database :
ID String
1 Test-1
2 Test-2
3 Test-3
4 Test-4
the query works fine it gives me the next record but not the id which i put like if i put id=1 returns id 2 or id=2 returns info for id 3 and the result is not accurate. So my question is what should I do id to return correct result not +1. I know it starts to count from 0 and i want to fix that in my script i want to make it to start from 1.
replace this
$id = 1;
by
$id = 0;
or replace this
SELECT * FROM records WHERE id > ?
by
SELECT * FROM records WHERE id >= ?
^--//-will look for id equal to 1 or bigger
why don't you change your query to
EDIT: Query will not work as records are deleted.
$stmt_1 = $conn->prepare("SELECT * FROM records WHERE id = ?");
$stmt_1->bindValue(1,$id +1);
$stmt_1->execute();
So you will get the record with the next highest id.
Just a hint to optimize your query.
One further question do you use auto_increment in your table definition. If you do so, you shold keep in mind that this mysql-sequence starts by one to calculate the primary key
Hope it helps
Related
I am having problems achieving the query to select data from a table in the db after a defined value has been met.
My code to do this is:
$fi = 'first_segment'
$im = popo.jpg
$sqls = "SELECT * FROM $fi,news_pictures
WHERE $fi.pi_id = news_pictures.pi_id
AND news_pictures.i_name = '$im'
GROUP BY news_pictures.id DESC";
I wasn't able to achieve the result with that query.
Basically, I want the query to confirm if news_pictures.i_name = '$im' and if true, return starts from the value of $im followed by other data in the table depending on news_pictures.id DESC.
The sample data and output:
Table news_pictures:
id i_name
-- ------
1 coco.jpg
2 lolo.jpg
3 popo.jpg
4 dodo.jpg
Since $im = popo.jpg, I want my query to display all values starting from popo.jpg with id DESC, i.e. popo.jpg, lolo.jpg, coco.jpg.
I got to solve the question with the help of a friend.
$fsqls = "SELECT * FROM $fi,news_pictures WHERE $fi.pi_id = news_pictures.pi_id AND news_pictures.i_name = '$im' GROUP BY news_pictures.id";
$rres = mysqli_query($connection, $fsqls) or print(mysqli_error($connection));
while($row = mysqli_fetch_assoc($rres))
{
$rnm = $row["id"];
}
$sqls = "SELECT * FROM news_pictures WHERE news_pictures.id <= $rnm ORDER BY news_pictures.id DESC";
I have the following table, named "question".
I want a query that will retrieve the maximum QID from the table where I specify a Pid. Then I want to take the selected value and add 1 to it. I want this to work even if there is no value where Pid is for example 1 or 2.
For example if the max qid is 1 where pid is 1, I want a code to retrieve a 1 and add 1. If I in my query put "WHERE pid=2" and there is no pid=2 in my table, I want it to make qid=1.
I have this code to get the maximum qid-value but it doesn't work.
$qu = mysqli_query("SELECT max(qid) AS id FROM answer_det WHERE WHERE pid = '1' ");
$result = $mysqli->query($qu);
if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {
echo $row['id'];
}
}
I think you should use ifnull
$qu = mysqli_query("SELECT ifnull(max(qid)+1, 1) AS id
FROM answer_det WHERE WHERE pid = '1' ");
In this way if you try a query for a pid that not exist you get the value 1 otherwise you get the value +1
I am writing a Custom Query in WordPress database to get the previous record from the posts table.
Example:
I have an ID of 34975; after I query the database I should get the ID as 34972, which is the previous record ID.
SQL
$results = $wpdb->get_results( "SELECT * FROM agencies_posts WHERE ID = '34975 ' LIMIT 1", OBJECT );
foreach( $results as $item ){
$previous_depature_port = $item->ID;
}
If I'm understanding your question correctly, you need to add ORDER BY and use < instead of =:
SELECT *
FROM agencies_posts
WHERE ID < 34975
ORDER BY ID DESC
LIMIT 1
Pretty sure you want:
select *
from agencies_posts
where id = (select max(id) from agencies_posts where id < '34975')
If the 'current' id is what's known and you just want the one prior.
Select everything with an id less than the one you are interested in, and only grab the first one
SELECT *
FROM agencies_posts
WHERE ID < '34975 '
ORDER BY ID DESC LIMIT 1"
I am trying to get the first id with a specific date.
So this is my code:
$checkm = "SELECT FIRST(id) FROM times WHERE date='2014-03-07'";
$resultm = mysqli_query($con,$checkm);
I have a table called times and there are some columns. Two of them are date and id.
I am trying to get the first row's id with the date 2014-03-07.
EDIT: Fixed!
$checkm = "SELECT id FROM times WHERE date='2014-03-06' ORDER BY id ASC LIMIT 1";
$resultm = mysqli_query($con,$checkm);
while($row = mysqli_fetch_array($resultm)) {
$resultm1 = $row['id'];
}
You probably want the minimum id.
SELECT min(id)
FROM times
WHERE date = '2014-03-07'
pretty straightforward...
SELECT id FROM times WHERE date='2014-03-07' ORDER BY id ASC LIMIT 1
I have a question probably lame but it made me stuck
I have the a db query
$query_Recordset10 = "SELECT * FROM products
WHERE razdel='mix' AND ID='$ID+1' AND litraj='$litri' ORDER BY ID ASC";
$Recordset10 = mysql_query($query_Recordset10, $victor) or die(mysql_error());
$row_Recordset10 = mysql_fetch_array($Recordset10);
$totalRows_Recordset10 = mysql_num_rows($Recordset10);
This is the query for the next product in the line based in the ID of the current product thats on the page.
But if the next product matching the criteria in the query is 2 or more ID's ahead my cycle breaks. So is there a way for skipping this rows and get the next ID matching the criteria.
Thank you very much.
SELECT * FROM products
WHERE razdel='mix' AND ID > $ID AND litraj='$litri' ORDER BY ID ASC";
PROBLEM SOLVED. I still have a lot to learn.
SELECT * FROM products WHERE razdel='mix' AND ID>'$ID' AND litraj='$litri' ORDER BY ID ASC LIMIT 1
this is the wright line but my mistake was how $ID is generated.
Thank you all.
Change your query to this:
$query_Recordset10 = "SELECT * FROM products
WHERE razdel='mix' AND ID > '$ID' AND litraj='$litri' ORDER BY ID ASC LIMIT 1";
So you still only get 1 row returned (if there's anything to return), but you'll be returning the next row (according to ORDER BY ID ASC) versus (potentially) the row with an incremental ID.
Use foreach to loop over an array rather than trying to access by indexes (if you're fetching every value):
foreach ($records as $record) {
}
instead of
for ($i = 0; $i < count($records); $i++) {
}
or
$i = 0;
while ($i < count($records)) {
$i++;
}
or
$i = 0;
do {
} while (++$i < count($records));
Use this query instead:
SELECT * FROM products WHERE razdel='mix' AND ID>$ID AND litraj='$litri' ORDER BY ID ASC LIMIT 1
This will give you the next element after the one with ID of $ID.