简体   繁体   English

如何获取查询结果到数组中传递的值(mySQL和PHP)

[英]How to get the results of a query to the values passed in in an array (mySQL and PHP)

I am working on a page where the user can choose which overdue invoices to download via a ZIP file. 我正在一个页面上,用户可以在该页面上选择要通过ZIP文件下载哪些过期发票。 I am changing an existing query to only pull out those invoices that have been checked by the user, where the job id numbers are sent in to the PHP function via an array. 我正在更改一个现有查询,以仅提取用户已检查的那些发票,其中作业ID号通过数组发送到PHP函数。

The existing query that downloads all the invoices is: 下载所有发票的现有查询为:

$sql = '
select i.jobid 
  from invoice i 
  join job j 
    on j.id = i.jobid 
 where i.total > 0
 ';

$Q = $this->read_db->query($sql, array($days));

$days is the number of days for which invoices are shown. $ days是显示发票的天数。

I have changed it to: 我将其更改为:

$ids = implode(',', $invids);  // $invids is the array sent in of invoice numbers
$sql = 'select `i`.`jobid` from `invoice` `i` WHERE `i`.`jobid` IN ' . $ids . ' join `job` `j` on `j`.`id` = `i`.`jobid` where `i`.`total` > 0 ';

$Q = $this->read_db->query($sql, array($days));

When I try and echo $Q->result(), I get an error Call to a member function result() on boolean . 当我尝试回显$ Q-> result()时, Call to a member function result() on boolean收到Call to a member function result() on boolean的错误Call to a member function result() on boolean I am fairly new to mySQL, so any help would be much appreciated. 我对MySQL相当陌生,因此不胜感激。

Your query is very strangely formatted and invalid, this is why it returned a boolean (false). 您的查询格式非常奇怪且无效,这就是为什么它返回布尔值(false)的原因。

SELECT i.jobid FROM `invoice` i JOIN `job` j ON j.id = i.jobid WHERE i.total > 0
SELECT i.jobid FROM `invoice` i JOIN `job` j ON j.id = i.jobid WHERE i.total > 0 AND i.jobid IN('.$ids.')

Additionally, please always use PDO or Prepared MySQLI. 此外,请始终使用PDO或Prepared MySQLI。

https://www.w3schools.com/php/php_mysql_prepared_statements.asp https://www.w3schools.com/php/php_mysql_prepared_statements.asp

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

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