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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀