我正在从我的数据库中查询大量代码,并且在用户可以将另一个代码输入数据库之前需要进行一些验证。
示例代码如下:
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/