2015-05-22 13 views
8
$sql = "SELECT sql_calc_found_rows * FROM members". 
     " ORDER BY username LIMIT :startRow, :numRows"; 

try { 
    $st = $conn->prepare($sql); 
    $st->bindParam(":startRow", $startRow, PDO::PARAM_INT); 
    $st->bindParam(":numRows", $numRows, PDO::PARAM_INT); 
    $st->execute(); 
} catch (PDOException $e) { 
    die("Query failed: " . $e->getMessage()); 
} 

Qui ottengo l'errore:errore PHP DOP quando si utilizza segnaposti nella clausola LIMIT di una query MySQL

Query failed: SQLSTATE[42000]: Syntax error or access violation: 1064 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 ''5'' at line 1.

Il LIMIT :startRow, :numRows ha problema a :numRows.

Ho provato sia $st->bindParam e $st->bindValue, entrambi non hanno funzionato.

+0

Puoi provare a rimuovere ° vuota tra ': startRow,: numRows' – Jens

+0

valore di $ ordine? –

+1

SELECT sql_calc_found_rows * FROM ?? cos'è questo? –

risposta

-2

L'ho risolto.I Digitare il segnaposto :numRows.

$numRows=(int)$numRows; $sql = 'SELECT sql_calc_found_rows * FROM ' . TBL_MEMBERS .'ORDER BY'. $order .'LIMIT :startRow,:numRows'; try { $st = $conn->prepare($sql); $st->bindValue(":startRow", $startRow, PDO::PARAM_INT); $st->bindValue(":numRows", $numRows, PDO::PARAM_INT); $st->execute();

E ha funzionato. Ho anche notato che il ' dovrebbe essere usato al posto di ".

0

Penso che il problema sia con TBL_MEMBERS Suppongo che questa sia una vista (subselect). Quindi, se avete tabella dei prodotti per esempio, e si desidera eseguire seguente dichiarazione:

select sql_calc_found_rows * from select id, code, name, slug, info from products order by code 

si riceverà seguente errore:

SQL Error (1064): 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 'select id, code, name, slug, info from products order by code' at line 1

Ma se si cambia query:

select sql_calc_found_rows * from (select id, code, name, slug, info from products) v order by code 

funzionerà.

In sintesi, TBL_MEMBERS è una vista che dovrebbe essere messo tra parentesi e dato alias (i mio esempio alias è 'v')

+0

'TBL_MEMBERS' è una definizione. 'define (" TBL_MEMBERS "," members ");' Non penso che sia questo il problema. –

0

vi consiglio di guardare il testo della query SQL ciò che produce in realtà DOP. Puoi farlo con l'aiuto di MySQL general query log.

Molto probabilmente, i tipi formali di $startRow e/o $numRows sono stringhe, non interi, e la conseguente domanda è quindi qualcosa di simile LIMIT '0', '5' (errore di sintassi), invece di LIMIT 0, 5 (corretta).

Il fatto è che, anche con PDO::PARAM_INT, quando il tipo formale del parametro non è intero (is_int restituisce false), PDO lo inserisce tra virgolette. Quindi, si deve lanciare parametri interi prima di loro legame (per esempio usando intval):

$st->bindParam(":startRow", intval(trim($startRow)), PDO::PARAM_INT); 
$st->bindParam(":numRows", intval(trim($numRows)), PDO::PARAM_INT); 
Problemi correlati