簡體   English   中英

帶有限制的php mysql select語句

[英]php mysql select statement with Limit

我不斷收到此錯誤,但此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;

}

在您的函數中嘗試此代碼

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM