Mysql query error limit - php

I write a website and use php. When ı write
$cat = mysql_query("
Select m.Image_link AS link
, m.Name
, m.ID
, c.Name AS catName
from movie m
join has_category mc
on m.ID = mc.movie_id
join category c
on c.ID = mc.category_id
where c.Name = '$category'
ORDER
BY ID desc"
);
this query everything is okay but when ı insert limit, ı take error.
#$sayfa=$_GET['s'];
$kacar=12;
$toplansayfa=ceil(mysql_num_rows($cat)/$kacar);
$baslangic=($sayfa * $kacar) - $kacar;
$bul_cat=mysql_query("
Select m.Image_link AS link
, m.Name
, m.ID
, c.Name AS catName
from movie m
join has_category Mc
on m.ID = mc.movie_id
join category c
on c.ID = mc.category_id
where c.Name = '$category'
ORDER
BY ID desc
limit $baslangic, $kacar
");
echo mysql_error();->
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-12, 12' at line 1
Where is my mistake..thanks for your interest.

As with many SQL programming errors, it helps a great deal to look at the actual text of the query you tried to run.
So, try this:
$query = "Select movie.Image_link AS link, movie.Name, movie.ID, category.Name AS catName from (movie inner join has_category on movie.ID=has_category.movie_id inner join category on category.ID=has_category.category_id) where category.Name='$category' ORDER BY ID desc limit $baslangic, $kacar";
$bul_cat = mysql_query($query); /*deprecated API! */
if (!$bul_cat) {
echo "query failed: " . $query;
echo mysql_error(); /* deprecated API! */
}
You will almost certainly spot the problem right away when you display the actual query you tried to run. I'm sure Paul Siegel's diagnosis is correct... your query says LIMIT -12,12. You Can't Do That™.

Related

MySQL - Inner Join Count from two tables

I have two tables:
Category
| id | name | case |
Items
| id | name | categoryid | <-- this is the relation column
I'm tryig to show all categorys with your respective number of items, like this:
Category Name: Abstract [15 items]
I'm using this code:
$getcategory = mysqli_query($con, "
SELECT c.name
, c.id
, c.case
, i.id
, COUNT(i.categoriaid) AS photos
FROM category c
JOIN items i
ON c.id = i.categoriaid
WHERE i.id != ''
ORDER
BY c.id ASC
");
while ($showcategory = mysqli_fetch_array($getcategory)) {
echo '
<div class="category-container">
<div class="category-title">'.$showcategory['name'].'</div>
<div class="category-img-container">
<div class="img-stretch"><img src="'.$showcategory['case'].'" alt=""/></div>
</div>
<div class="category-count"><div><span class="destaque alto">[ '.$showcategory['photos'].' ]</span> telas</div></div>
</div>
';
}
But this don't work. What's wrong?
The query doesn't look right. There's an aggregate in the SELECT list, and no GROUP BY clause. If the query returns a result, it's going to be a single row. And it doesn't make sense to return non-aggregates in the SELECT list, if we're just going to return a COUNT.
Assuming that id is the PRIMARY KEY (or a UNIQUE KEY) in the category table, if sql_mode doesn't include ONLY_FULL_GROUP_BY, we might be able to get this to run:
SELECT c.name
, c.id
, c.case
, MIN(i.id) AS min_id
, MAX(i.id) AS max_id
, COUNT(i.categoriaid) AS photos
FROM category c
LEFT
JOIN items i
ON i.categoriaid = c.id
AND i.id <> ''
GROUP BY c.id
ORDER BY c.id
If sql_mode includes ONLY_FULL_GROUP_BY, we can either use aggregate functions (e.g. MIN or MAX) around c.name and c.case, or we can extend the GROUP BY clause to include those columns.
SELECT c.name
, c.id
, c.case
, MIN(i.id) AS min_id
, MAX(i.id) AS max_id
, COUNT(i.categoriaid) AS photos
FROM category c
LEFT
JOIN items i
ON i.categoriaid = c.id
AND i.id <> ''
GROUP
BY c.id
, c.name
, c.case
ORDER
BY c.id
Without a specification, we're just guessing. There could any number of things "wrong" with this statement. As a example list: invalid table references, invalid column references (is there a column name categoriaid in the items table? I took that from the SQL in the question).
I suggest you take the SQL writing to another environment and get the SQL written and tested. Then bring that statement back into your PHP code. (Divide and conquer.)
Also, in the PHP code, we can test whether query execution was successful, by testing the return from mysqli_query. If it's FALSE, then we know it wasn't successful, and we can retrieve the error with mysqli_error function.
$sql = "SELECT ... ";
if( !$getcategory = mysqli_query($con,$sql) ) {
// statement execution was not successful
die mysqli_error($con);
}

mysql limit not work orderby with limit 0,5

$selectorders=sprintf("SELECT s.stud_rollno, s.admissiondate, s.fname, s.lname, s.gender, c.communityname, t.name, y.yearname, s.iname
FROM erp.student s inner join year y
on year_id = s.ayear
inner join community c
on c.d_id = s.community
inner join types t
on t.id = s.department
ORDER BY s.stud_rollno
limit 0,10");
$results = mysql_query($selectorders) or die(mysql_error());
$tot_rsselect = mysql_num_rows($results);
my runtime error is
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 200' at line 1
but run in localhost/phpmyadmin page.
I think you are trying to select only top 10 rows. So in this scenario there is no need to use 0, 10 or like somthing. You can just simply write LIMIT 10
So your query will be:-
"SELECT s.stud_rollno, s.admissiondate, s.fname, s.lname, s.gender, c.communityname, t.name, y.yearname, s.iname
FROM erp.student s inner join year y
on year_id = s.ayear
inner join community c
on c.d_id = s.community
inner join types t
on t.id = s.department
ORDER BY s.stud_rollno
limit 10";
I'm not sure but this might be your solution.

datatable into cakephp join tables

I have problem with datatables and script from
http://datatables.net/development/server-side/php_cake to cakephp.
I using it many time but no I have to use join and server response me
MySQL Error: 1052.
Problem is in this line :
$sTable = "`clients` AS c JOIN users AS u ON (c.user_id = u.id)";
When I print my all query and paste it in phpmyadmin everything is OK, but cake get error.
My generated SELECT :
SELECT SQL_CALC_FOUND_ROWS c.id, c.name, c.nip, c.adress, c.tel,
c.tel2, c.email, c.created, c.id
FROM clients c join users u ON(c.user_id = u.id)
ORDER BY c.id asc
LIMIT 0, 10
#edit
I modificated my script and now it generate something like this:
SELECT SQL_CALC_FOUND_ROWS
c.id,c.name,c.nip,c.adress,c.tel,c.tel2,c.email,c.created,c.id
FROM clients AS c join users AS u ON c.user_id = u.id
ORDER BY `c`.`name` asc
LIMIT 0, 10
But it's still not work. (I used char '`' before prefix and after and col name. )
you need create a view with the relationship!!
Create the VIEW in MySql:
CREATE VIEW 'the_view' AS
SELECT
a.id,
a.num_factura_proveedor,
b.nombre_comercial
FROM compras a
INNER JOIN terceros b ON a.tercero_id = b.id
on the server side script:
$sTable = "the_view";
//the columns of the view
$aColumns = array('id' , 'num_factura_proveedor', 'nombre_comercial');
$sIndexColumn = "id";

How do I use MySQL Left Join to get results based on another table row's id?

I am building a custom forum in CodeIgniter. I have four primary tables: parents(categories), children(boards), threads, and messages(replies). The thread table only contains the thread id, author, title, date, and the id of the first message. When a thread is viewed, it takes the column "first_msg_id" to retrieve the content of the thread.
I would like to create a query that gets the count of all the replies in a specific board. Basically, any message that doesn't match a thread's first_msg_id field. Here is what I have so far:
$query = "
SELECT
m.message_id, m.thread_id
t.thread_id, t.first_msg_id
FROM forum_messages AS m
LEFT JOIN forum_threads AS t ON t.first_msg_id != m.message_id
WHERE t.child_id = ".$board_id."
ORDER BY m.message_id";
This is the error I'm getting:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.thread_id, t.first_msg_id FROM forum_messages AS m ' at line 3
Here are my tables:
Update
Ok, so now that I got my query working(well, fixed the error), it's not doing what I want... I think I have my logic wrong. I wrote a script that created 40 new threads in one board. None of them have replies. When I use mysql num rows with the query I made, I get a result of 1560. It should return 0. Why is this happening??? Lol.
Got it working.
You are missing a comma after m.thread_id
Something like
SELECT
m.message_id, m.thread_id,
t.thread_id, t.first_msg_id
FROM forum_messages AS m
LEFT JOIN forum_threads AS t ON t.first_msg_id != m.message_id
WHERE t.child_id = ".$board_id."
ORDER BY m.message_id
Comma missing , after m.thread_id
Try with -
$query = "SELECT m.message_id, m.thread_id, t.thread_id, t.first_msg_id ....
More appropriate -
$query = "
SELECT
m.message_id, m.thread_id,
^
-------------------------|
t.thread_id, t.first_msg_id
FROM forum_messages AS m
LEFT JOIN forum_threads AS t ON t.first_msg_id != m.message_id
WHERE t.child_id = ".$board_id."
ORDER BY m.message_id
";

MySQL Query w/ 2 tables to get the desired results

I am trying to pull in the related postings based on the posting category. So where all category ids match the category id field.
Additional clarification:
I've been experimenting all morning and still no luck, and this is where I am at now. Note the $CatID in the ON clause is from a previous query above this one and the value is correct.
$sql = "
(SELECT
a.id,
a.Price,
a.City,
a.Country,
a.Title,
a.Description,
a.Category, // contains the corresponding ads_cate.id.
a.recdate,
c.cateName,
'item' AS type FROM ads_list AS a
LEFT OUTER JOIN ads_cate AS c
ON $CatID=a.Category
WHERE to_days(now())<=(to_days(recdate)+14)
ORDER BY RAND())
";
And as tested:
echo $CatID . $row['Category']; // Outputs 3 3 which is correct. Category is 3 ads_cate id is also 3 for this record.
My results is pulling in duplicates and ALL ads regardless of Category.
If every ad has a category, and assuming your ads_cate table has an id field:
$sql = "
SELECT
a.id,
a.Price,
a.City,
a.Country,
a.Title,
a.Description,
a.Category, // contains the corresponding ads_cate.id.
a.recdate,
c.cateName,
'item' AS type
FROM ads_list AS a
LEFT OUTER JOIN ads_cate AS c
ON c.id=a.Category
WHERE to_days(now())<=(to_days(recdate)+14)
AND a.Category = $CatID
ORDER BY RAND()
";
Although I don't understand your question, when using join, you can use SELECT DISTINCT to stop the duplicates. Beyond that, I don't understand the question.
This is my working code. Had to modify some based on bfavaretto's suggestion, but it's working as expected now:
$sql = "
(SELECT
a.id,
a.Price,
a.City,
a.Country,
a.Title,
a.Description,
a.Category,
a.images,
a.recdate,
a.images,
a.image2,
a.image3,
a.image4,
a.imgWidth,
a.imgHeight,
a.ftype,
c.id,
c.cateName,
a.email,
'item' AS type FROM ads_list
AS a LEFT OUTER JOIN ads_cate
AS c ON c.id=a.Category WHERE to_days(now())<=(to_days(recdate)+14) AND a.Category = $CatID ORDER BY RAND())";

Categories