简体   繁体   English

我可以在 Mysql 查询中使用 PHP function 吗?

[英]Can I use PHP function within Mysql Query?

Following is my query and I would like to call PHP substr();以下是我的查询,我想调用 PHP substr(); within Mysqlquery.在 Mysqlquery 中。 Apparently, substr(ident.secondName, -2, 2) is working, but substr(ident.FirstName, 0, 2) is not working.显然, substr(ident.secondName, -2, 2) 正在工作,但 substr(ident.FirstName, 0, 2) 不工作。 Thank you in an advance!预先感谢您!

mysqli_query($con, "select * from ident where ident.FirstName LIKE '%$first_name%' 
OR substr(ident.FirstName, 0, 2) = '$first_two' OR ident.FirstName IS NULL AND
(ident.SecondName LIKE '%$second_name%' OR substr(ident.SecondName, -2, 2) 
= $second_two' OR ident.SecondName IS NULL)");

No you cannot use a PHP function in a MySQL query, see the MySQL function reference for strings instead. 不,您不能在MySQL查询中使用PHP函数,请参阅MySQL函数参考中的字符串 MySQL also happens to have a SUBSTR function which is why the first case works. MySQL也恰好具有SUBSTR函数,这就是第一种情况起作用的原因。

不可以。您不能在mysql中使用PHP函数。

You're going to pass the query to you Mysql instance, it won't recognize the PHP code. 您将把查询传递给您的Mysql实例,它将无法识别PHP代码。 So it's a no. 所以这不是。

您可以为此使用子字符串功能

Use MySQL function SUBSTRING(): http://www.w3resource.com/mysql/string-functions/mysql-substring-function.php or use MySQL variables: How to declare a variable in MySQL? 使用MySQL函数SUBSTRING(): http : //www.w3resource.com/mysql/string-functions/mysql-substring-function.php或使用MySQL变量: 如何在MySQL中声明变量?

If you want to do it with PHP you will have to get the data first to fill variables and apply functions. 如果要使用PHP进行操作,则必须首先获取数据以填充变量并应用函数。

Why not use function 'SUBSTRING' in MySql? 为什么不在MySql中使用函数“ SUBSTRING”?

SUBSTRING('stackoverflow', 1, 5)
result : stack

SUBSTRING('stackoverflow', -13, 5)
result : stack

SUBSTRING('stackoverflow', 6)
result : overflow

SUBSTRING('stackoverflow', -8)
result : overflow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM