Visualizer7
Visualizer7

Reputation: 325

How to know a certain row exists MySQL

I want to prevent users from commenting on deleted threads. I've set a variable (let's call it 'tnum') to identify the original thread and its comments, so I can display them in a same web page. When I delete a thread, the original thread and all comments get deleted at once (delete from ~ where tnum is ~)

So I think I can prevent comment submission on deleted threads using that.

I want to put out an error message when there is no row in table with certain tnum value.

if( 'some code' ) {

error("There is no data for the thread.");

}

Could someone help me with this?

Thanks.

Upvotes: 1

Views: 87

Answers (2)

liquorvicar
liquorvicar

Reputation: 6126

You can use COUNT() in MySQL to get the number of rows that match your criteria. So something like this

$db = new PDO( /* connection details */ );
$sth = $db->prepare( 'SELECT COUNT(*) AS numRows FROM `table` WHERE tnum = :tnum' );
$sth->execute( array( 'tnum' => $tnum ) );
$result = $sth->fetchAll();
if( $result['numRows'] == 0 ) {
    error("There is no data for the thread.");
}

Upvotes: 1

aleroot
aleroot

Reputation: 72686

To know if a column exist in a table you can run this query :

SHOW COLUMNS FROM `table` LIKE 'fieldname';

Upvotes: 0

Related Questions