Search code examples
phpmysqlpdo

Get specific columns and compare values php


I want to compare two values from two columns but it doesn't work correctly. Do I need to cast the results and what I am doing wrong?

$query = "SELECT `column1`, `column2` FROM `table` WHERE `id` = ? ";
$stmt = $conn->prepare($query);
$stmt->bindParam(1,$eventID);
$stmt->execute();
$currentJoin = (int)  $stmt->fetchColumn();
$maxParticipants = (int) $stmt->fetchColumn(1);

if ($currentJoin >= $maxParticipants) {
    return;
}
else {

Solution

  • Warning There is no way to return another column from the same row if you use PDOStatement::fetchColumn() to retrieve data. - fetchColumn()

    You can use fetch() instead

    $stmt->execute();
    $result = $stmt->fetch();
    if($result[0] >= $result[1] ){
        // ...
    }