1

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.

Strawberry
  • 33,750
  • 13
  • 40
  • 57
  • 1
    You can not set a negative limit (or offset). Check your calculations. However - the problem is probably that `$sayfa` is not a number, and thus converted to `0`. – Paul Spiegel Apr 30 '17 at 22:13
  • 3
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Apr 30 '17 at 22:13
  • 1
    **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. Your user data is **not** [properly escaped](http://bobby-tables.com/php.html) and there are [SQL injection bugs](http://bobby-tables.com/) and can be exploited. – tadman Apr 30 '17 at 22:22
  • 1
    Does anybody know where people newly learning php/MySQL are finding their way to the long-dead `mysql_` API? They can't be looking at the php online manual: it's full of warnings. It's happening so much that there must be some major obsolete tutorial information out there. Anybody know where? I suppose part of the problem is grossly incompetent and lazy professors stealing their students' future. – O. Jones Apr 30 '17 at 22:27
  • Thanks for your suggestions, but i am a student and its my homework. I dont have enough time learn PHP7, i have only 2 weeks. Infact its my database homework but i try improving this task with php. – Osman Tıkna Apr 30 '17 at 22:29
  • 1
    @O.Jones Since you asked... There's quite a few such links, for example [this](https://www.tutorialspoint.com/mysql/mysql-select-query.htm), which show pretty high up in the search results. It's sad, but even quite a few college textbooks (that I've seen previously) offer _only one_ way of writing queries in PHP, which is using `mysql_*()` functions. – Dhruv Saxena Apr 30 '17 at 22:40

1 Answers1

0

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™.

O. Jones
  • 103,626
  • 17
  • 118
  • 172