I used mysqli_fetch_array and the count was correct until I changed to fetch (), which now only returns the total number of rows instead of returning each number for each row.
So, for the first line, I want an echo of "1", etc.
NEW NOTE . Everything else inside the while statement returns the correct values, except for the counter, which returns the total number of rows, while I want the row number in the order in which it was selected from the sql statement.
In accordance with the request. This is my connection.
I do not know if I should check "$ e-> getMessage ();" per request, since I use this connection for all my requests.
try { $dbh = new PDO('mysql:host=localhost;dbname=my_db;charset=utf8', 'usr', 'pwd', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) ); } catch (PDOException $e){ echo $e->getMessage(); }
It worked
$query = mysqli_query($con, 'SELECT * FROM music'); $count = 0; while($row = mysqli_fetch_array($query)){ $count++; echo $count; }
New does not work.
$query = $dbh->query('SELECT * FROM music'); $count = 0; while($row = $query->fetch()){ $count++; echo $count; }
sql php mysql pdo counter
user3109875
source share