将变量推入数组后使用implode的问题

时间:2021-05-05 19:18:27

I am currently testing how to use a simple implode for a mysql query after I have pushed the variables into the array. I just can't get around the error, I know it says, invalid arguments, but the array has been set up and I know it worked in another part of my page with an almost identical code.. I guess there are somewhere missing some ' or " or . or but no matter what I change it doesn't work.

我正在测试如何在将变量推入数组后使用简单的内爆来进行mysql查询。我只是无法解决错误,我知道它说,无效的参数,但数组已经设置,我知道它在我的页面的另一部分工作与几乎完全相同的代码..我想有一些地方缺少一些'或'或。或者无论我改变它什么都行不通。

I appreciate any help!

我感谢任何帮助!

Here is the part where I set up the array:

这是我设置数组的部分:

$LFBsubjects = Array();
$LFBsubjects[] = $dataset2['subject1'];
$LFBsubjects[] = $dataset2['subject2'];

And the output I have printed via print_r is:

我通过print_r打印的输出是:

Array ( [0] => Mathematics [1] => English ) 

Now comes the query, which uses the implode function:

现在来了查询,它使用了implode函数:

$SelectTSubjectsQuery = "
SELECT subject_id FROM subjects
WHERE subject IN (".implode(',', $LFBSubjects).")";

$statement = $pdo->query($SelectTSubjectsQuery);

The error is:

错误是:

Warning: implode(): Invalid arguments passed in /var/www/xxx/html/lfb.php on line 626

1 个解决方案

#1


0  

Invalid argument error means you need to use quotes between string for MYSQL QUERY like IN ("test") You can use as like:

无效的参数错误意味着您需要在字符串之间使用MYSQL QUERY之类的引号,如IN(“test”)您可以使用如下:

$values = implode("','", $LFBsubjects);    
$SelectTSubjectsQuery = "    SELECT subject_id FROM subjects    WHERE subject IN ('".$values."')";

Explanation:

说明:

Your array consists on string values when you use IN RANGE in MYSQL for string values than you must need to pass it in quotes.

当您在MYSQL中使用IN RANGE作为字符串值时,您的数组包含字符串值,而不是您必须在引号中传递它。

Basic example:

基本示例:

$SelectTSubjectsQuery = "
SELECT subject_id FROM subjects
WHERE subject IN ('val1','val2')";

Update 1

更新1

After checking your comments, you are using wrong variable name in implode

检查您的注释后,您在implode中使用了错误的变量名称

$LFBSubjects

This should be this:

这应该是这样的:

$LFBsubjects // with small s

#1


0  

Invalid argument error means you need to use quotes between string for MYSQL QUERY like IN ("test") You can use as like:

无效的参数错误意味着您需要在字符串之间使用MYSQL QUERY之类的引号,如IN(“test”)您可以使用如下:

$values = implode("','", $LFBsubjects);    
$SelectTSubjectsQuery = "    SELECT subject_id FROM subjects    WHERE subject IN ('".$values."')";

Explanation:

说明:

Your array consists on string values when you use IN RANGE in MYSQL for string values than you must need to pass it in quotes.

当您在MYSQL中使用IN RANGE作为字符串值时,您的数组包含字符串值,而不是您必须在引号中传递它。

Basic example:

基本示例:

$SelectTSubjectsQuery = "
SELECT subject_id FROM subjects
WHERE subject IN ('val1','val2')";

Update 1

更新1

After checking your comments, you are using wrong variable name in implode

检查您的注释后,您在implode中使用了错误的变量名称

$LFBSubjects

This should be this:

这应该是这样的:

$LFBsubjects // with small s