SELECT ... LIMIT 0,1 syntax error - php

for($nr = 0; $nr < 2; $nr++){
print $nr; print(gettype($nr)); // prints 0integer
$result = mysqli_query($con,"SELECT * FROM phcdl_files
ORDER BY file_id DESC LIMIT '$nr',1")
or die(mysqli_error($con));
}
Trying to run the query above but I'm having troubles because of syntax.
Running it on PhpMyAdmin with Limit 0,1 works good however
Any idea what's the problem?

Try with -
"SELECT * FROM phcdl_files ORDER BY file_id DESC LIMIT $nr,1"

I think the issue is that you're adding quote around the 0.
Your SQL query should look like:
"SELECT * FROM phcdl_files ORDER BY file_id DESC LIMIT $nr, 1"

remove single quotation of $nr veriable from query
QUERY = "select * from tb_name order by id desc limit $nr , 1"

Related

Creating a subquery with mysqli in PHP to fetch array last 10 results in ascending order

I thought this would be simple but I'm having a tough time figuring out why this won't populate the the data array.
This simple query works fine:
$queryPrice = "SELECT price FROM price_chart ORDER BY id ASC LIMIT 50";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
But instead I want it to choose the last 10 results in Ascending order. I found on other SO questions to use a subquery but every example I try gives no output and no error ??
Tried the below, DOESN'T WORK:
$queryPrice = "SELECT * FROM (SELECT price FROM price_chart ORDER BY id DESC LIMIT 10) ORDER BY id ASC";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
I also tried specifying the table name again and using the IN, also doesn't work:
$queryPrice = "SELECT price FROM price_chart IN (SELECT price FROM price_chart ORDER BY id DESC LIMIT 10) ORDER BY id";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
In both examples my array is blank instead of returning the last 10 results and there are no errors, so I must be doing the subquery wrong and it is returning 0 rows.
The subquery doesn't select the id column, so you can't order by it in the outer query. Also, MySQL requires that you assign an alias when you use a subquery in a FROM or JOIN clause.
$queryPrice = "SELECT *
FROM (SELECT id, price
FROM price_chart
ORDER BY id DESC LIMIT 10
) x ORDER BY id ASC";
$resultPrice = mysqli_query($conn, $queryPrice) or die (mysqli_error($conn));
$data = array();
while ($row = mysqli_fetch_assoc($resultPrice)) {
$data[] = $row['price'];
}
You would have been notified of these errors if you called mysqli_error() when the query fails.
Your second query is the closest. However you need a table alias. (You would have seen this if you were kicking out errors in your sql. Note you will need to add any field that you wish to order by in your subquery. In this case it is id.
Try this:
SELECT * FROM (SELECT price, id
FROM price_chart ORDER BY id DESC LIMIT 10) as prices
ORDER BY id ASC
You must have errors, because your SQL queries are in fact incorrect.
First, how to tell you have errors:
$resultPrice = mysqli_query (whatever);
if ( !$resultprice ) echo mysqli_error($conn);
Second: subqueries in MySQL need aliases. So you need this:
SELECT * FROM (
SELECT id, price
FROM price_chart
ORDER BY id DESC LIMIT 10
) AS a
ORDER BY id ASC";
See the ) AS a? That's the table alias.

SQL Select latest row where value matches

I'm trying to return the row from my database, with the highest UID, where the URL column matches http://urltocheck.com.
I've tried all manner of things I can think of, and this is the closest I can get, but I'm getting an SQL syntax error.
My Table is called Adam, and I have the columns... UID (unique), URL (plus loads more). I'm trying to access the MySQL databse via PHP.
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY `UID` ASC;
LIMIT 1;";
Can anyone help please?
You shoul use order DESC and remove the ";" after ASC
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY `UID` DESC
LIMIT 1";
Try like this. Also, remove ; at this line ORDER BY UID ASC; (didn't noticed that earlier) because of which limit 1 not coming to picture.
SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
and `UID` = (select max(`uid`) from `Adam`)
with the highest UID
You should order by UID desc and limit to 1.
You can also ORDER BY MAX ID.
<?php
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY MAX(`UID`) DESC;";
This is executed faster.
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY MAX(`UID`);";
?>

MySQL LIKE and LIMIT

I am trying to select stuff from a database with the LIKE statement, but I would also like to LIMIT the amount of records I get out of it.
$query = mysqli_query($connect,
"SELECT * FROM proizvodi WHERE `naziv` LIKE %127% ".
"AND LIMIT $start, $per_page");
The code I have is a boolean, and is not working. How to fix this?
The LIMIT requires quotation marks and cannot be joined with AND.
SELECT * FROM proizvodi WHERE `naziv` LIKE '%127%' LIMIT $start, $per_page
Look at the documentation for the SELECT statement, you don't need to use AND to join your WHERE and LIMIT sections.
See: http://dev.mysql.com/doc/refman/5.7/en/select.html
SELECT * FROM proizvodi WHERE `naziv` LIKE %127% LIMIT $start, $per_page;
SQL is not guaranteed to return the same results in the same order each time, unless you use an order by. You code should look like:
SELECT *
FROM proizvodi
WHERE `naziv` LIKE %127%
ORDER BY <something>
LIMIT $start, $per_page
You can try something like:
$sql='SELECT * FROM product WHERE `ProName` LIKE "%'.$searchWord.'%" LIMIT '.$this_page_first_result.', '.$results_per_page.';'
Limit is not part of the where clause and therefore no AND is allowed.
$query = mysqli_query($connect,
"SELECT * FROM proizvodi
WHERE `naziv` LIKE %127%
ORDER BY naziv ASC
LIMIT 10");
This will limit your queries up to 10.
$start, $per_page is something to get hands later if this is a problem now.. :)

Sorting SQL query with the submitting of a form

I have a general understanding of what I would like to do but not sure how to write the SQL.
Users have the ability from changing the sort from ASC to DESC and increase the query limit from 5 to 10.
$result=$mysqli->query("SELECT * FROM table WHERE status = 3 ORDER BY name ASC LIMIT $start_from, 5");
the option box for asc/desc will end up being $sort_order
the option box for limit will be $limita
I tried to write it like
$result = "SELECT * FROM wp_pod_tbl_bars WHERE status = 3";
if(!empty($limita)){$result.="LIMIT $limita, 5";}
if ($result = $mysqli->query($result)) {
while($row = $result->fetch_object()){
but the query ended up empty due to the query being split into different lines.
Any idea what I am doing wrong? ill post more code if needed but this is generally where my issue is.
Put space between status and LIMIT.
$result = "SELECT * FROM wp_pod_tbl_bars WHERE status = 3";
if(!empty($limita)){$result.=" LIMIT '".$limita."', 5";}
---------------^

PHP / SQL Query and PHP Variables

I have this query below:
$msgg = mysql_query("SELECT *
FROM mytable
WHERE time>$time
AND id='someid'
ORDER BY id ASC
LIMIT $display_num",$myconn);
see this line: AND id='someid' <-- someid ...
OK, the query above returns 2 results as expected...
Now for the problem....
-- I have a variable myVar and it's content is "someid" (without quotes)...same as the string 'someid'
When I do this:
$msgg = mysql_query("SELECT *
FROM mytable
WHERE time>$time
AND id=myVar
ORDER BY id ASC
LIMIT $display_num",$myconn);
See: myVar <-- this variable contans .. someid
The second query returns no results.
Update: When using ... AND id='$myVar' it sees $myVar as empty for some reason.
Put a $ in front of myVar:
$msgg = mysql_query(
"SELECT *
FROM mytable
WHERE time > $time
AND id = '$myVar'
ORDER BY id ASC
LIMIT $display_num", $myconn
);
You forgot the dollar sign and the single quotations:
AND id='$myVar'
Additionally, you may want to consider using heredoc:
$query = <<<MYSQL
SELECT *
FROM mytable
WHERE time>$time
AND id='$myVar'
ORDER BY id ASC
LIMIT $display_num
MYSQL;
$msgg = mysql_query($query, $myconn);

Categories