0

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'");
?>
2
  • LIMIT but no ORDER BY? Kinda odd. Commented Oct 7, 2015 at 11:26
  • 2
    @jarlh - Depends on if the table is large or small - or if he wants ordered by default primary key. Order By is not necessary and lack of it is not really that odd depending on use case. Commented Oct 7, 2015 at 11:30

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

Try this: (another way in setting Limit and Offset)

<?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 $per_page OFFSET $start'");
?>

And BTW you misplaced a single quote near LIMIT.

3 Comments

Another member of the try this club Please explain WHY we should try this. Try this answers get put in the low quality queue and are often deleted
Better, but you only get a half corona
@Hard Spocker I tried this code but getting error "Undeclared variable OFFSET"
0

You are making the creation of your query much too complicated and therefore missing some simple errors. Remember that when you use double quoutes " in PHP it will expand $variables into the string for you.

So a simpler and easier to read and therefore debug method would be this

$sql = "SELECT * FROM experts 
        WHERE exp_process LIKE '%$process%' 
          AND exp_machinaries like '%$machineCat%' 
          AND exp_country = '$country' 
        LIMIT $start, $Sper_page";

$sResults = $oCon->dbFetchSmarty($sql);

Comments

0

You left out a space between LIMIT and '. It must've been

$country."' '   LIMIT  .  ........

Comments

0

For best solution to check query is in phpmyadmin. You just echo query and copy/past query section into phpmyadmin. so it will give a proper guidance for your structure of query or any error for same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.