php - PDO query to display on id information -


i have following table:

tbl_users ============== id username password email 

i have following code id's of users , want display id field. can't work.

$db = $db_con;   $stmt =  $db->prepare("select * tbl_users");           $result = $stmt->execute();  while ($row = $result->fetchall(pdo::fetch_assoc)){   echo $row['id']; } 

i keep getting following error:

notice: undefined index: id in 

fetchall returns all of rows. shouldn't call inside loop.

you can solve in different ways. here one:

// entire dataset $rows = $result->fetchall(pdo::fetch_assoc));  // loop through array foreach ($rows $row) {   echo $row['id']; } 

another way (the 1 intended write), fetch rows 1 one using fetch instead of fetchall:

while ($row = $result->fetch(pdo::fetch_assoc)) {   echo $row['id']; } 

by way, if you're going need id, it's best practise select id yourtable instead of selecting using *. make query (and page) faster, , decrease load on database.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -