Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select query not showing error but not showing result either

Status
Not open for further replies.

madcelt

Programmer
Oct 31, 2020
2
0
0
GB
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?
 
Hi

madcelt said:
However when I add the following snippet no errors are reported but no output is shown either:
Because a warning would be reported :
PHP Warning: PDOStatement::execute() expects parameter 1 to be array, string given

So change the [tt]PDOStatement::execute()[/tt]'s parameter into array :
Code:
[navy]$stmt[/navy][teal]->[/teal][COLOR=orange]execute[/color][teal]([highlight][[/highlight][/teal][navy]$id[/navy][teal][highlight]][/highlight]);[/teal]

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


Feherke.
feherke.github.io
 
Thanks a million for picking that up. Of course it works perfectly after making the change you suggested. Much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top