×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Select query not showing error but not showing result either

Select query not showing error but not showing result either

Select query not showing error but not showing result either

(OP)
I'm probably being stupid and missing an obvious error in my code. However, I can't spot it so would appreciate any help available.
This code works fine:

CODE -->

<?php
include-once ('config.php');
    $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset;port=$port";
    try {
        // create a PDO connection with the configuration data
        $conn = new PDO($dsn, $user, $dbpassword);
        // display a message if connected to database successfully
            if($conn) {
            echo "Connected to the " . $dbname . " database successfully!<br>";
            }
       } catch (PDOException $e) {
        // report error message
        echo $e->getMessage();
       }       
$sql = "SELECT COUNT(*) AS num FROM checks";
$stmt = $conn->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo '<br>' . $row['num'] . ' users exist.';
?> 

Connection to the database is confirmed and the row count is correctly reported. However when I add the following snippet no errors are reported but no output is shown either:

CODE

$id = '108';  // for testing - live $id comes from login form
$sql = "SELECT * FROM checks WHERE ID = ?";
$stmt = $conn->prepare($sql); 
$stmt->execute($id);
$users = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->errorCode() == 0) {
  while(($users = $stmt->fetch()) != false) {
    echo $users['checks'] . "\n";
  }
} else {
    $errors = $stmt->errorInfo();
    echo($errors[2]);
} 

I'd appreciate any help anyone can offer on this please?

RE: Select query not showing error but not showing result either

Hi

Quote (madcelt)

However when I add the following snippet no errors are reported but no output is shown either:
Because a warning would be reported :

Quote:

PHP Warning: PDOStatement::execute() expects parameter 1 to be array, string given

So change the PDOStatement::execute()'s parameter into array :

CODE --> PHP ( fragment )

$stmt->execute([$id]); 

Make sure you have error reporting turned on while debugging : error_reporting(E_ALL);.

Feherke.
feherke.github.io

RE: Select query not showing error but not showing result either

(OP)
Thanks a million for picking that up. Of course it works perfectly after making the change you suggested. Much appreciated.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close