MySQL query using an array

前端 未结 4 2019
野的像风
野的像风 2020-11-30 15:46

I\'m trying to query a MySQL database using an array but I\'m having trouble!

I have a table called clients, I want to be able to select \'name\' from all rows whose

4条回答
  •  暖寄归人
    2020-11-30 16:23

    The second query should use $thelist not $row, and it should be outside of the while loop. The foreach loop is unnecessary when processing a single row. You can access the name in $row with a simple $row[0]. Something like this (untested):

    $query1 = "SELECT name FROM clients WHERE sector = '$sectorlink'";
    $clientresult = mysql_query($query1, $connection) or trigger_error("SQL", E_USER_ERROR);
    
    while($row = mysql_fetch_array($clientresult)){
        $temp[] = '"'.$row[0].'"';
    }
    
    $thelist = implode(",",$temp);
    $query = "SELECT count(*) FROM studies WHERE client IN ($thelist) ORDER BY (date) desc";
    $result = mysql_query($query, $connection) or trigger_error("SQL", E_USER_ERROR);
    

    Caution: Please be aware that your code is highly vulnerable to SQL injection attacks. It's fine for testing or internal development but if this code is going to be running the Fort Knox web site you're going to want to fix it up quite a bit. Just an FYI. :-)

提交回复
热议问题