doutan8506 2019-01-27 15:12
浏览 63
已采纳

从另一个表的数组中的table where子句中选择

I'm trying to insert a failsafe into a code to prevent them from going a step further, and I've been scratching my head for a while now. I am able to put something into an array, but I am not able to get the items from the array to match the second select query. I only get Array instead of the value from the item.

My first select query is this:

$datohenter3 = "select DATE_FORMAT(datotid, '%Y.%m.%d') AS dato from gramorapport34 group by dato order by dato asc";
$hentdatoer = $db->query($datohenter3);

$periodedatoer = array();
for ($x = 1; $x <= $db->affected_rows; $x++) {
    $periodedatoer[] = $hentdatoer->fetch_assoc();
}

Then I want to match the values from this array with my next select query:

$rapportdatoer = "select fradato, tildato from gramorapportlogg WHERE  fradato IN('".$periodedatoer."') OR tildato IN('".$periodedatoer."')";
$rapportdatoeksist = $db->query($rapportdatoer);
if ( !$rapporteksist ) die('Database Error: '.$db->error);
    while($row = mysqli_fetch_array($rapportdatoeksist))
     {
        print_r($row);
     } 

The errors I am getting are:

Notice: Array to string conversion for the second select

Database Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' fradato IN('Array') OR tildato IN('Array')' at line 1

I'm not an expert in JOIN SELECT queries. This is using MariaDB 10.3.12 with PHP7.2

var_dump available at: https://www.lokalradio.no/rapport/gramo/datohenttest.php

  • 写回答

1条回答 默认 最新

  • dstew32424 2019-01-27 15:47
    关注

    Notice that $periodedatoer is an array of array. Each element inside has 1 key of dato (as you var_dump displays).

    So use array-column to get the values and then implode as:

    $rapportdato = implode("','", array_column($periodedatoer, "dato"));
    

    Now you can use $rapportdato in your second query as:

    $rapportdatoer = "select fradato, tildato from gramorapportlogg WHERE  fradato IN('" . $rapportdato . "') OR tildato IN('" . $rapportdato . "')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog