2011-11-28 88 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