-1

I have the following piece of code. Drives me crazy. Cannot find where is my mistake. Looks like there is a conflict with the use of limit in my sql syntax and the mysql_query in variable $result does not return anything.

<?php

$sql = " select  *  from  `share`  where  (`category` LIKE '$category' and `country` LIKE '$country' and `city` LIKE '%$city_name%' and `job_title` LIKE '%$job_name%' and `user_id`!='$session_user_id')  
        order by `share_id` desc LIMIT 3; ";

$result = mysql_query($sql);

if( mysql_num_rows($result) ==0 ){ 
  echo"success";
}

?>
user2491321
  • 665
  • 8
  • 31
  • `$result = mysql_query($sql) or die(mysql_error());` see if that yields any errors. – Funk Forty Niner Nov 20 '14 at 18:39
  • Given that SQL injection vulnerability, it's not surprising that the query results in errors. Literally any SQL code could be trying to execute. – David Nov 20 '14 at 18:43
  • I change it to $result = mysql_query($sql) or die(mysql_error()); gives me Undeclared variable: 3LIMIT – user2491321 Nov 20 '14 at 18:45
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You will also want to [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 20 '14 at 19:24

1 Answers1

0

String concatenation problem that causing issues.

$sql = "select * from `share`
        where
          (`category` LIKE '". $category ."'
            and `country` LIKE '". $country ."'
            and `city` LIKE '%". $city_name ."%'
            and `job_title` LIKE '%". $job_name ."%'
            and `user_id` != '". $session_user_id ."')
        order by `share_id` desc
       limit 3"; 
thecodeparadox
  • 84,459
  • 21
  • 136
  • 161