我不断收到此错误,但此 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/