You are running multiple queries with PDO, which is usually not supported.
The first query consists of set @row_number = 0;
(note the ;
at the end of statement) and the second query is
SELECT @row_number:=@row_number+1 AS row_number,il.*
from itemloja il order by il.LojaId";
Your options are a) use PHP to count row by row or b) enable multiple queries in PDO.
Add a counter through PHP
// Query simples, sem vários statements
$Query = "SELECT il.* FROM itemloja il ORDER BY il.LojaId";
$go = $pdo->prepare($Query);
$go->execute();
$results = $go->fetchAll(PDO::FETCH_ASSOC);
$i = 0;
foreach($results as $it){
$it->rowNumber = ++$i;
}
Working with Multiple Queries with PDO_MYSQLND
Content originally posted by Sam Dark(modified by Your Common Sense) in the Stackoverflow English and translated by myself
To execute multiple queries you will need
- PHP 5.3+
- Mysqlnd
- Emulator of Prepared Statement. Rest assured that
PDO::ATTR_EMULATE_PREPARES
is configured as 1
(default). Another option is to use $pdo->exec
directly.
exec
$db = new PDO("mysql:host=localhost;dbname=test", 'root', '');
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
$sql = "set @row_number = 0;
SELECT @row_number:=@row_number+1 AS row_number,il.*
from itemloja il order by il.LojaId";
try {
$db->exec($sql);
} catch (PDOException $e) {
echo $e->getMessage();
die();
}
statements
$db = new PDO("mysql:host=localhost;dbname=test", 'root', '');
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
$sql = "set @row_number = 0;
SELECT @row_number:=@row_number+1 AS row_number,il.*
from itemloja il order by il.LojaId";
try {
$stmt = $db->prepare($sql);
$stmt->execute();
} catch (PDOException $e) {
echo $e->getMessage();
die();
}
Post more code that involves calling $go->fetchAll. Show us where you define the value of the $go variable so we can better analyze your problem.
– Marco Aurélio Deleu
@Marcoauréliodeleu done, the error happens there in the last line same
– Luan Gabriel