php 如何检查 MySQL 数据库中是否存在值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11292468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:12:00  来源:igfitidea点击:

How to Check if value exists in a MySQL database

phpmysqlexists

提问by Hatim

Suppose I have this table:

假设我有这个表:

id | name | city
------------------
1  | n1   | c1
2  | n2   | c2
3  | n3   | c3
4  | n4   | c4

I want to check if the value c7exists under the variable cityor not.

我想检查c7变量下是否存在该值city

If it does, I will do something.
If it doesn't, I will do something else.

如果是这样,我会做些什么。
如果没有,我会做其他事情。

回答by Reinder Wit

preferred way, using MySQLi extension:

首选方式,使用 MySQLi 扩展:

$mysqli = new mysqli(SERVER, DBUSER, DBPASS, DATABASE);
$result = $mysqli->query("SELECT id FROM mytable WHERE city = 'c7'");
if($result->num_rows == 0) {
     // row not found, do stuff...
} else {
    // do other stuff...
}
$mysqli->close();

deprecated:

弃用:

$result = mysql_query("SELECT id FROM mytable WHERE city = 'c7'");
if(mysql_num_rows($result) == 0) {
     // row not found, do stuff...
} else {
    // do other stuff...
}

回答by Blaster

For Exact Match

精确匹配

"SELECT * FROM yourTable WHERE city = 'c7'"

For Pattern / Wildcard Search

对于模式/通配符搜索

"SELECT * FROM yourTable WHERE city LIKE '%c7%'"

Of course you can change '%c7%'to '%c7'or 'c7%'depending on how you want to search it. For exact match, use first query example.

当然,您可以更改'%c7%''%c7''c7%'取决于您要搜索的方式。对于完全匹配,请使用第一个查询示例。

PHP

PHP

$result = mysql_query("SELECT * FROM yourTable WHERE city = 'c7'");
$matchFound = mysql_num_rows($result) > 0 ? 'yes' : 'no';
echo $matchFound;

You can also use ifcondition there.

您也可以if在那里使用条件。

回答by Hari

Assuming the connection is established and is available in global scope;

假设连接已建立并且在全局范围内可用;

//Check if a value exists in a table
function record_exists ($table, $column, $value) {
    global $connection;
    $query = "SELECT * FROM {$table} WHERE {$column} = {$value}";
    $result = mysql_query ( $query, $connection );
    if ( mysql_num_rows ( $result ) ) {
        return TRUE;
    } else {
        return FALSE;
    }
}

Usage: Assuming that the value to be checked is stored in the variable $username;

用法:假设要检查的值存储在变量$username中;

if (record_exists ( 'employee', 'username', $username )){
    echo "Username is not available. Try something else.";
} else {
    echo "Username is available";
}

回答by Hassam Satti

For Matching the ID:

匹配ID:

Select * from table_name where 1=1

For Matching the Pattern:

匹配模式:

Select * from table_name column_name Like '%string%'

回答by Muhammad Raheel

SELECT
    IF city='C7'
    THEN city
    ELSE 'somethingelse'
    END as `city`
FROM `table` WHERE `city` = 'c7'

回答by mushkincode

I tried to d this for a while and $sqlcommand = 'SELECT * FROM database WHERE search="'.$searchString.'";';
$sth = $db->prepare($sqlcommand); $sth->execute(); $record = $sth->fetch(); if ($sth->fetchColumn() > 0){}
just works if there are TWO identical entries, but, if you replace if ($sth->fetchColumn() > 0){}with if ($result){}it works with only one matching record, hope this helps.

我尝试了一段时间, 如果有两个相同的条目, 它就可以工作,但是,如果你 用它替换它, 它只适用于一个匹配的记录,希望这会有所 帮助。 $sqlcommand = 'SELECT * FROM database WHERE search="'.$searchString.'";';
$sth = $db->prepare($sqlcommand); $sth->execute(); $record = $sth->fetch(); if ($sth->fetchColumn() > 0){}
if ($sth->fetchColumn() > 0){}if ($result){}