用于检查MySQL表中的字段是否等于1的函数

时间:2010-09-13 09:26:10

标签: php mysql

对于下面的MySQL表,PHP函数只会测试'subcheck'是否等于1?

提前致谢,

约翰

`submission` (
  `submissionid` int(11) unsigned NOT NULL auto_increment,
  `loginid` int(11) NOT NULL,
  `title` varchar(1000) NOT NULL,
  `slug` varchar(1000) NOT NULL,
  `url` varchar(1000) NOT NULL,
  `displayurl` varchar(1000) NOT NULL,
  `datesubmitted` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `subcheck` tinyint(1) NOT NULL,
  PRIMARY KEY  (`submissionid`)
) 

1 个答案:

答案 0 :(得分:0)

您没有提供所需的信息,例如表名,检查条件,所以我只是给您一个简单的查询供您使用...

$query = mysql_query('SELECT subcheck FROM your_table WHERE subcheck = "1"');

if ($query)
{
//your subcheck is 1
}
else
{
//your subcheck is not 1 / nothing was found
}

如果您只需要选择具有特定提交内容的提交者,则最好将其更改为以下内容:

$submissionid = 809;
$query = mysql_query('SELECT subcheck
                      FROM your_table
                      WHERE submissionid = "' . $submissionid . '"');

if ($row = mysql_fetch_row($query))
{
    if ($row[0] == 1) { } //your subcheck is one
    else { } //your subcheck is not one
}
else { } //no matching records found

如果它来自一个不安全的来源,例如用户输入,请记住转义$ submissionid!