PDO/PHP - Check if row exist

Related searches

I want to have a condition incase the row doesn't exist at all.

$stmt = $conn->prepare('SELECT * FROM table WHERE ID=?');
$stmt->bindParam(1, $_GET['id'], PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);

Tried if (count($row) == 0) and if($stmt->rowCount() < 0) but none of them works.

You can just check the return value directly.

$stmt = $conn->prepare('SELECT * FROM table WHERE ID=?');
$stmt->bindParam(1, $_GET['id'], PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);

if( ! $row)
{
    die('nothing found');
}

/*
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // Same here
if( ! $rows)
{
    die('nothing found');
}
*/

If you are asking about checking without fetching then simply have MySQL return a 1 (or use the COUNT() command).

$sql = 'SELECT 1 from table WHERE id = ? LIMIT 1';
//$sql = 'SELECT COUNT(*) from table WHERE param = ?'; // for checking >1 records
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $_GET['id'], PDO::PARAM_INT);
$stmt->execute();

if($stmt->fetchColumn()) die('found');

PDO/PHP - Check if row exist, You can just check the return value directly. $stmt = $conn->prepare('SELECT * FROM table WHERE ID=?'); $stmt->bindParam(1, $_GET['id'],� In this tutorial, I will be showing you how to check if a row exists in MySQL. To do this, I will be using PHP’s popular PDO object. First of all, let’s assume that we have a users table and that we want to check if a given email address already exists in our database.

if($stmt->rowCount() == 0) 

should work fine, since the number of rows can't be less than zero in any event at all.

From the manual:

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.

I would suggest reading up on that here.

Check if a MySQL row exists using PHP's PDO object., $row = $stmt->fetch(PDO::FETCH_ASSOC); //If num is bigger than 0, the email already exists. In the PHP example above, we: Connected to our MySQL database using PDO. PHP PDO check if row exists or found Counting or checking if your PDO MySQL query found a row relies on one key object called rowCount. rowCount returns the number of rows affected by the last executed query. With this knowledge when you do a SELECT WHERE query the amount of rows that qualify the statment can be returned with rowCount.

Heres what I use in my object classes:

function exists_by_id () {
    // check if object exists by id
    $stm = DB::$pdo->prepare('select count(*) from `table` where `column`=:column');
    $stm->bindParam(':column', $this->column);
    $stm->execute();
    $res = $stm->fetchColumn();

    if ($res > 0) {
        return true;
    }
    else {
        return false;
    }
}

check if SQL row exists with PHP. Ask Question Asked 9 years ago. If you would like to use PDO (PHP Data Object), as alex suggested, then use the following code -

Hold on a minute. Why would you have duplicate usernames in the first place. Usernames should be unique. It's not username in the sense of login username.

Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

- it checks if one row exists in the table or not, according to the WHERE clause. - it returns all (or only a selection of) fields for that one row, if exists. Disadvantages: - it doesn't return row fields reliable if more than one row found.

Comments
  • $stmt->rowCount() should be what you need, but checking if it is less than 0 won't help - it will be equal to 0 or less than 1
  • @DaveRandom The docs state that not all drivers cause SELECT to give a rowCount. Apparently you should actually be using columnCount.
  • @arxanas A fair point, but frankly running two queries just to get the row count (as the manual suggests) is a sucky solution. Even $rowCount = count($rows = $stmt->fetchAll()) is a better solution than that, I feel - and it will still result in 0 if there are no rows.
  • Your solution will work, but it is not true that PDOStatement::fetch returns an array; it returns the next result or false if there are no rows anymore. And that's why $row is false when there is an empty result set.
  • @matthias.p, thanks for the correction. I never use fetch() so my statement about an empty array was from fetchAll()
  • Thanks! that's what i was looking for. Since I knew the result was return in an array, I was thinking it could be the best way to check the variable instead of using a function or doing another sql query.
  • Can I use rowCount() instead of fetchColumn() in the if condition?
  • @Sajad the manual says no: "Returns the number of rows affected by the last SQL statement"
  • As arxanas already said, you should not use rowCount to detect the rows returned by a select statement.
  • If you're using MySQL it shouldn't be a problem, but otherwise you're completely correct.
  • oh then I was using the wrong sign. I thought if it does exist when its greater, I should just reverse it to less than 0 ! lol
  • Interesting function.