php - 比较从 mySql 查询返回的字符串

标签 php mysql string

我正在从我的数据库中查询大量代码,并且在用户可以将另一个代码输入数据库之前需要进行一些验证。

示例代码如下:

TD-BR-010212-xxxxxxxx

其中TD代表促销,BR代表地点,数字代表日期,其余随机。

我的问题是,在将代码输入数据库之前,我想检查该代码的日期和地点是否已经存在,因为不应该允许他们从相同的地点和日期输入代码。

我假设它会是一个循环中的东西,因为我已经有了:

$location_part_of_td = $code[2].$code[3];
$date_part_of_td = $code[4].$code[5].$code[6].$code[7].$code[8].$code[9];

$trade_day_result = mysql_query('SELECT * from wp_scloyalty WHERE promotion_type = trade-day') or die(mysql_error());  // Pulls all trade day codes from the database and checks the date part of the code. 

// the date part exists with the same area part, user cant redeem.
while($info = mysql_fetch_array( $trade_day_result )) 
{ 

     $code = $info["product"];

} 

但我只是不确定检查字符串的最佳方法..

最佳答案

您可以使用 MySQL LIKE 子句在您的数据库中获取与您的代码相似的条目。

例子:

$code_exists = mysql_query( 
      "SELECT 'a' FROM table_name WHERE column_name LIKE 'TD-BR-010212-%'"
);
if(mysql_num_rows($code_exists) > 0) {
    // The specified place/date is taken
} else {
    // No promotion at place BR on the specified date.
}

“%”在 SQL LIKE 子句中用作通配符。

关于php - 比较从 mySql 查询返回的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9737473/

相关文章:

php - 如何在mysql中使用like运算符搜索带有特殊字符的记录

c - 如何改进这种trim-half-string算法?

刷新时重新生成 PHP session_id

mysql - Web 统计表的数据库字段

linux - 打开大型 sql 文件并更改一些内容以导入到 MySQL NDB 集群

mysql - 在 MySQL 上结合 INSERT INTO 和 SELECT

.net - 如何从 VB.NET 中的枚举中获取描述?

android - 如何使用字符串作为代码?

php - 使用格式化的键和值将所有 MYSQL 记录传递到数组中

javascript - 在 js 中使用 ajax 的联系表单不会 POST