Search code examples
phpsqllimitsql-like

Sql error while using "LIMIT".


I'm getting error "Undeclared variable: $start" while using the SQL query below.

<?php
  if($Spage == ""){
     $Spage = "1";
  }
  $Sper_page = "5"; 
  $start = ($Spage-1)*$Sper_page;
  $sResults = $oCon->dbFetchSmarty("SELECT * FROM experts WHERE exp_process LIKE '%".$process."%' AND exp_machinaries like '%".$machineCat."%' AND exp_country = '". $country."' 'LIMIT $start, $Sper_page'");
?>

Solution

  • You have messed your single quotes. Should be

    $sResults = $oCon->dbFetchSmarty("SELECT * FROM experts WHERE exp_process LIKE '%".$process."%' AND exp_machinaries like '%".$machineCat."%' AND exp_country = '". $country."' LIMIT $start, $Sper_page");
    

    And cleaning up the query a bit:

    $sResults = $oCon->dbFetchSmarty("SELECT * FROM experts WHERE exp_process LIKE '%$process%' AND exp_machinaries like '%$machineCat%' AND exp_country = '$country' LIMIT $start, $Sper_page");
    

    Next step to remove would be to use prepared statements and bind these parameters in