2011-11-28 103 views
2

我不断收到这个错误,但是这个sql语句在mysql中可以正常工作,可能有些愚蠢,但是我在整个周末都无法工作。php mysql select语句限制

select e.* 
, t.nombre 
, c.compania 
, c.nombre 
, c.apellido 
, c.ruc 
, c.direccion 
from envio e 
inner join cliente c on e.cliente_id = c.id 
inner join transporte t on t.id = e.transporte_id 
limit 0, 10; 

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 'limit 0, 10' at line 11 

这里的后续代码var_dump结果:

string(263) "select e.* , t.nombre as transporte_nombre , c.compania , c.nombre , c.apellido , c.ruc , c.direccion from envio e inner join cliente c on e.cliente_id = c.id inner join transporte t on t.id = e.transporte_id limit 0, 10" 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 'limit 0, 10' at line 11 

这里是我的代码,我现在用的2班这个1是我的数据库连接,工作正常(与其他网页进行测试),另一个是一个返回选择。

if(!empty($_REQUEST['page'])) 
    $page = $_REQUEST['page']; 
else 
    $page = "1"; 

$limit = 10;  
if($page) 
    $start = ($page - 1) * $limit; 
else 
    $start = 0; 

require_once("../class/sql.php"); 
$db = new MySQL(); 

require_once("../class/envios.php"); 
$envios = new Envio(); 
var_dump($envios->BuscaEnvios("",$start,$limit)); 

if(is_array($conditions)) 
    $consulta = $db->consulta($envios->BuscaEnvios($conditions,$start,$limit)); 
else 
    $consulta = $db->consulta($envios->BuscaEnvios("",$start,$limit)); 

if($db->num_rows($consulta) > 0) 
    $total_pages = $db->num_rows($consulta); 
else 
    $total_pages = 0; 

在课堂上 “Envios”:

public function BuscaEnvios($conditions, $start, $limit) 
{ 
     $sql = "select 
       e.* 
      , t.nombre as transporte_nombre 
      , c.compania 
      , c.nombre 
      , c.apellido 
      , c.ruc 
      , c.direccion 
     from envio e 
     inner join cliente c on e.cliente_id = c.id 
     inner join transporte t on t.id = e.transporte_id "; 

     if($conditions != "") 
      $sql .= ' where '. implode(' AND ', $conditions); 

     $sql .= " limit $start, $limit"; 

     return $sql; 

} 
+0

极限0,10基本上是说 “返回0行,开始于第10行”。我想你想要他们换个方法吗? – GordonM

+0

尝试删除分号 – knittl

+1

@GordonM不正确,0(开始),10(行)。 – Prisoner

回答

0

试试这个代码在函数

if(sizeof($conditions)>0) 
      { 
      $sql .= " where ".implode(' AND ', $conditions); 
      } 

     $sql .= " limit $start, $limit"; 
+0

这工作,但我在调用函数之前使用sizeof($条件)。所以,当我去那个功能,我只是检查$条件!=“”:) 感谢大家,因为每个评论让我更接近修复它! – Andres

+0

@Andres如果条件是一个数组,为什么不只是说'if(count($ conditions))'? – 2011-11-29 01:02:52

+0

-1对于丑陋的极限偏移使用情况,是不是会限制$ limit offset $ start更有意义? (并且是标准的SQL) – ajreal