Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why this sql not working?

Tags:

php

phpunit

I have a query

public static function TestQuery(

 $start=0,
 $limit=0){


 $sql = "
SELECT     count(*) AS total
FROM    db.table1
JOIN    db.table2
 ON     table1.fieldID = {$fieldID}

AND    table2.assigned = 'N'";



  $qry = new SQLQuery;
  $qry->query($sql);
  if($row = $qry->fetchRow()){
   $total = intval($row->total);
  }

 return $total;

} 

which works fine but when I add the limit as below, then it doesnt work and gives me errors

public static function TestQuery(

 $start=0,
 $limit=0){


 $sql = "
SELECT     count(*) AS total
FROM    db.table1
JOIN    db.table2
 ON     table1.fieldID = {$fieldID}

AND    table2.assigned = 'N'";

//this fails   
if($recordlimit > 0) $sql .= "LIMIT {$startRecord}, {$recordLimit} ";  
//  
  $qry = new SQLQuery;
  $qry->query($sql);
  if($row = $qry->fetchRow()){
   $total = intval($row->total);
  }

 return $total;

} 

Any help will be appreciated

like image 411
Asim Zaidi Avatar asked Nov 24 '25 14:11

Asim Zaidi


2 Answers

Put a space in front of LIMIT:

" LIMIT {$startRecord}, {$recordLimit} "

without the space you sql will result in a syntax error.

Edit: This is answer is not correct! MySQL will not error without a space before LIMIT (however, earlier versions of phpmyadmin will incorrectly parse such sql).

like image 185
webbiedave Avatar answered Nov 26 '25 02:11

webbiedave


Your variables are called $limit and $start:

if($limit > 0) $sql .= " LIMIT {$start}, {$limit} "; 
like image 27
Mark Byers Avatar answered Nov 26 '25 04:11

Mark Byers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!