带有限制的php mysql select语句

标签 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 个类,一个是我的数据库连接,它工作正常(用其他页面测试过),另一个是返回选择的那个。

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";

关于带有限制的php mysql select语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8297299/

相关文章:

php - 谷歌地图 "but your computer or network may be sending automated queries"错误

php - 如何将值数组插入到 mysql 表的不同列中?

java - 使用 volley 从 php 页面发送和接收

mysql - 在 phpmyadmin 中创建触发器时出错

python - 似乎无法让 Python、Peewee、pymysql 和 MySql 数据库正常工作,没有 'returning_clause' 错误

PHP/mysql - 我的时间戳不喜欢在大于 (>) 语句中

php - 基于 "visible"项目的 sql 项目编号

javascript - 当用户在表单上的文本框中键入内容时如何显示建议或相关名称

PHP SQL 查询 2 个日期之间

php - 如何将 mysql 不同查询写入 Elasticsearch