0

I have a database that I am trying to query to get information to display to my user. I have used

fetch(PDO::FETCH_ASSOC)

before when retrieving a single row or

$row = mysql_fetch_array($result)

with good results. However, it is my understanding that it is better practice to use PDO so that is what I am trying to do.

The problem I am running into is that my results are only showing me the first row of the data I need. In this instance it is displaying the column header over and over and never giving me the data.

$stmt = $conn->prepare("SELECT ? FROM application");
$stmt->bindparam(1, $application_ID);

$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);

foreach($results as $row){
    echo $row['application_ID'];
}

Here are the results

     application_IDapplication_IDapplication_IDapplication_ID
1
  • By the way. I have spent about 2 hours looking for a solution to this issue. So, if this is something that is out there already or is super easy then I apologize. Thanks for the help. Commented Aug 22, 2014 at 5:04

2 Answers 2

1

It is good that you are aware that MySQL has been oficially deprecated and now we are supposed to used MySQLi or better yet, PDO.

Since you are not accepting any user input, there is no need of using a prepared statement.

Simply do this:

$stmt = $conn->query("SELECT * FROM application");

$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach($results as $row){
   echo $row['application_ID'];
  //output other rows here
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. That makes sense what you are saying about not having to use prepared statement in this instance. Can you tell me how I would be able to make this work if I were? I have a feeling that I am would need an output param in my binding. Is that right?
You are welcome. If you were to pass a param, you'd do something like this $stmt = $conn->prepare("SELECT * FROM application WHERE id = :id "); then pass the parameter as $stmt->bindParam(':id', $id); and then execute it $stmt->execute().
0

As per the php documentation pdo::fetchAll

you have to use fetchAll, instead of fetchall.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.