Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9050051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:33:25+00:00 2026-06-16T12:33:25+00:00

I have a php script that selects data via mysql_, however recently I have

  • 0

I have a php script that selects data via mysql_, however recently I have been reading that PDO is the way to go and that mysql_ is becoming depreciated. Now I am converting that script to PDO.

My question is though, I am not using $_POST to select. I just want to select the entire table with all of its data so I enter this query :

$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $query->fetchall(); // or you can just $result = $query as hakre proposed!

so then like I did with my old depreciated mysql_ version of the script I used the echo to echo a table with the data in it.

    echo 
    "<table border='2'>
    <tr>
    <th>ID</th>
    <th>A Number</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Why</th>
    <th>Comments</th>
    <th>Signintime</th>
    </tr>"
    ;

    foreach($result as $row)
    {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td><a href=Student.php?studentA_num=" . $row['anum'] . ">" .$row['anum'] . " </a></td>";
  echo "<td>" . $row['first'] . "</td>";
  echo "<td>" . $row['last'] . "</td>";
  echo "<td>" . $row['why'] . "</td>";  
  echo "<td>" . $row['comments'] . "</td>";
  echo "<td>" . $row['signintime'] . "</td>";
  echo "<td> <input type=\"button\" value=\"Start Session\"onClick=\accept.php?id=" . $row['id'] . "&start=true></td>";
}

  echo "</tr>";
  echo "</table>";

now using this, I can not get a single output to my table.

This is all that is being outputted

My question is am I missing something from my select statements? Or am I not fetching any rows? Also I the connection settings set in another script called connect.php that is required by init.php (at the top of all of my pages)

Edit : 1

Edited the code so it now works, also adding a picture to show others how it should look! Hopefully some one can put this to some sort of use! This is how it looks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:33:27+00:00Added an answer on June 16, 2026 at 12:33 pm

    You are doing too much actually:

    $query = $dbh->prepare("SELECT * FROM students");
    $query->execute();
    $result = $dbh->query($query);
    

    The problematic line is:

    $result = $dbh->query($query);
    

    Check with http://php.net/pdo.query, the parameter is a string, actually the SQL string you already use above, not the result value of a PDO::prepare() call.

    For your simple query you can just do:

    $result = $dbh->query("SELECT * FROM students");
    

    Or if you like to prepare:

    $query = $dbh->prepare("SELECT * FROM students");
    $query->execute();
    $result = $query;
    

    The later is some boilerplate if you want to insert variables into the query, that’s why you prepare it.


    The next problem is with the foreach line:

    foreach($result as $row);
    

    You are terminating the loop immediately because of the semicolon ; at the end. Remove that semicolon so that the following angle-bracketed code-block becomes the body of the foreach-loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that selects some data from mysql and throw it
I have a php script that inserts data from an Android app into a
I have php script that creates a temporary watermark image for users that are
I have a php script that watches for the newest json file in a
I have a php script that grabs links from another website. I am storing
I have a PHP script that very much like a commenting system, but requires
I have a PHP script that opens an XML document, gets the DOM and
I have a PHP script that creates large complex tables. I am attempting to
I have a PHP script that executes a .bat file using system(cmd /c C:\dir\file.bat);
I have a php script that backup my table in .sql format and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.