简体   繁体   English

如何知道某一行存在 MySQL

[英]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.我已经设置了一个变量(我们称它为“tnum”)来识别原始线程及其评论,因此我可以在同一个 web 页面中显示它们。 When I delete a thread, the original thread and all comments get deleted at once (delete from ~ where tnum is ~)当我删除一个线程时,原始线程和所有评论都会立即被删除(从 ~ tnum 所在的位置删除 ~)

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.当表中没有具有特定 tnum 值的行时,我想发出一条错误消息。

if( 'some code' ) {

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

}

Could someone help me with this?有人可以帮我吗?

Thanks.谢谢。

You can use COUNT() in MySQL to get the number of rows that match your criteria.您可以在 MySQL 中使用 COUNT() 来获取符合条件的行数。 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.");
}

To know if a column exist in a table you can run this query:要知道表中是否存在列,您可以运行此查询:

SHOW COLUMNS FROM `table` LIKE 'fieldname';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM