带有Limit的php mysql select语句

时间:2011-11-28 14:27:13

标签: php mysql

我一直收到这个错误,但是这个sql语句在mysql中运行正常,它可能是一个愚蠢的东西,但我整个周末都没有工作。

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;

}

1 个答案:

答案 0 :(得分:0)

在你的函数中尝试这段代码

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

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