MySQL LIKE and LIMIT - php

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.. :)

Related

display more that one category from mysql

When I put an item in the database I can chose different "Used" categories .
For example I have Used, Barely Used, Rough. I am wanting all them to display on the same page.
Right now I have this code
$SQL_GetEquipment = "SELECT * FROM `new_equip` WHERE `condition`='Used' $SQLCat $Limit";
Is there a way to do that?
...where (`condition`='Used' OR `condition`='Barely Used' OR `condition`='Rough')...
Try this..
$SQL_GetEquipment = "SELECT * FROM `new_equip` WHERE `condition` LIKE '%Used' OR `condition` LIKE '%Barely Used%' OR `condition` LIKE '%Rough%' $SQLCat $Limit";
Check this link you simply use OR condition it will help you
$SQL_GetEquipment = "SELECT * FROM new_equip WHERE condition='Used' or condition='Barely Used' OR condition='Rough' $SQLCat $Limit";

SELECT ... LIMIT 0,1 syntax error

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"

Not Able to use ORDER by RAND() LIMIT 4 after users.user_id=".$_SESSION['user_id']

I am not able to use
order by rand() limit 5
after
['user_id']
Is there any way to do that, please help.
$sqlstate = "select * from questions, users where users.category_id=questions.category_id and users.user_id=".$_SESSION['user_id'] order by rand() limit 5;
You just need to fix the string and concatenation.
$sqlstate = "select * from questions, users where users.category_id=questions.category_id and users.user_id=".$_SESSION['user_id']." order by rand() limit 5";
Alternately, since you seem to be using PHP, you can just use the variable inside the double quotes:
$sqlstate = "select * from questions, users where users.category_id=questions.category_id and users.user_id=$_SESSION['user_id'] order by rand() limit 5";

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`);";
?>

Adding AVG(Price) into a mySQL query?

I have the following query
$query = "SELECT * FROM used ".$conditionString." ORDER BY id DESC LIMIT $offset, $rec_limit";
But now I want to know also the average price of the returned rows
AVG(Price) AS PriceAverage
How to do this?
this should work for you:
$query = "SELECT *,AVG(Price) FROM used ".$conditionString." ORDER BY id DESC
GROUP BY id WITH ROLLUP
LIMIT $offset, $rec_limit";
demo: http://sqlfiddle.com/#!2/4938f/1/0

Categories