gpt4 book ai didi

How to Check if value exists in a MySQL database(如何检查MySQL数据库中是否存在值)

转载 作者:bug小助手 更新时间:2023-10-28 09:34:26 27 4
gpt4 key购买 nike



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 c7 exists under the variable city or not.

我想检查变量City下是否存在值c7。



If it does, I will do something.

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

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


更多回答
优秀答案推荐

using modern MySQLi:

使用现代MySQLi:


$mysqli = new mysqli(SERVER, DBUSER, DBPASS, DATABASE);

$city = 'c7';
$result = $mysqli->execute_query("SELECT id FROM mytable WHERE city = ? LIMIT 1", [$city]);
if($result->num_rows == 1) {
// found
}

using legacy mysqli

使用旧版mysqli


$mysqli = new mysqli(SERVER, DBUSER, DBPASS, DATABASE);

$city = 'c7';
$stmt = $mysqli->prepare("SELECT id FROM mytable WHERE city = ? LIMIT 1");
$stmt->bind_param("s", $city);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows == 1) {
// found
}

using PDO:

使用PDO:


$pdo = new PDO($dsn, $user, $pass, $options);

$city = 'c7';
$stmt = $pdo->prepare("SELECT id FROM mytable WHERE city = ? LIMIT 1");
$stmt->execute([$city]);
if($stmt->rowCount() == 1) {
// found
}


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



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


You can also use if condition there.

你也可以在这里使用if条件。



This works for me :

这对我很管用:



$db = mysqli_connect('localhost', 'UserName', 'Password', 'DB_Name') or die('Not Connected');
mysqli_set_charset($db, 'utf8');

$sql = mysqli_query($db,"SELECT * FROM `mytable` WHERE city='c7'");
$sql = mysqli_fetch_assoc($sql);
$Checker = $sql['city'];

if ($Checker != null) {

echo 'Already exists';

} else {

echo 'Not found';
}



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


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=“’.$earch String.‘”;’;$sth=$db->prepare($SQLCOMMAND);$sth->Execute();$Record=$sth->fetch();if($sth->fetchColumn()>0){}只有在有两个相同的条目时才起作用,但是,如果您用if($Result){}替换if($sth->fetchColumn()>0){},希望它只对一个匹配的记录起作用。



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";
}


use

使用


select count(city) from mytable where city = 'c7'

This will send only a single value from the query. If it is 0 it is not present else it is present. Since you will not be use the other column values.

这将仅从查询中发送单个值。如果它是0,则它不存在,否则它存在。因为您不会使用其他列值。



For Matching the ID:

匹配ID:



Select * from table_name where 1=1


For Matching the Pattern:

若要匹配模式:



Select * from table_name column_name Like '%string%'


$result = mysqli_query($conn, "SELECT * FROM WHERE = ''";
$found = mysqli_num_rows($result) > 0 ? 'Yes' : 'no' ;
echo $found;

$RESULT=MYSQLI_QUERY($CONN,“SELECT*FROM WHERE=‘’”;$FOUND=mySQLI_NUM_ROWS($Result)>0?‘yes’:‘no’;ECHO$FOUND;



Here's is what worked with me if you're using PHP Version 8

如果您使用的是PHP版本8,下面是我使用的方法


$result = mysqli_query(database name, "SELECT id FROM mytable WHERE city = 'c7'");      

if(mysqli_num_rows($result) == 0) {
// Do Something
}
else {
// Do something else
}

The difference with my answer and Reindeer Wit is the 'i' in mysqli_query and mysqli_num_rows()

My Answer和Redeer Wit的不同之处在于mySQLI_QUERY和mySQLI_NUM_ROWS()中的‘I’


Also including the name of the database name inside of mysqli_query() that you are using.

还包括您正在使用的mySQLIQUERY()中的数据库名称。


更多回答

Wouldn't be better to use SQL's EXISTS statement like stackoverflow.com/a/1676573/204634 ?

使用像STACKOVERFLO.com/a/1676573/204634这样的SQL的EXISTS语句不是更好吗?

Speed-wise, would it be faster by adding LIMIT 1?

在速度方面,增加限制1会更快吗?

@IanY. that depends on the query but usually yes. Have a look at this: stackoverflow.com/questions/455476/…

@IanY。这取决于查询,但通常是肯定的。看看这个:Stackoverflow.com/Questions/455476/…

Use id instead of * (star). And PDO or MYSQLi should be used.

使用id而不是*(星号)。应使用PDO或MYSQLi。

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

您的答案可以通过其他支持信息来改进。请编辑以添加更多详细信息,如引用或文档,以便其他人可以确认您的答案是正确的。你可以在帮助中心找到更多关于如何写出好答案的信息。

"I figured that Reinder Wit answer is a bit outdated" — It isn't. It has two sections. The first section is marked preferred and is supported from PHP 5 onwards. You were trying to follow the section marked deprecated which is not supported since PHP 7.

“我认为Reinder的机智答案有点过时了”--它没有。它有两个部分。第一部分被标记为首选,并且从PHP5开始受支持。您正在尝试遵循标记为已弃用的部分,该部分从PHP7开始不再受支持。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com