简体   繁体   English

如何在MySQL中将数组作为参数传递给存储过程?

[英]How to pass array as parameter to stored procedure in mysql?

I'm trying to pass array as parameter to my stored procedure. 我试图将数组作为参数传递给我的存储过程。 But I'm not getting expected result. 但是我没有得到预期的结果。

I'm trying to send list of emails and ID's from view on button click to controller, and then I'm trying to fetch data from database by passing that list as parameter to my stored procedure. 我试图将按钮单击视图中的电子邮件和ID列表发送到控制器,然后试图通过将该列表作为参数传递给我的存储过程从数据库中获取数据。

Here is my code: 这是我的代码:

View: 视图:

<form action="<?= base_url('certificate/sendMail') ?>" method="post">
    <table id="example1" class="table table-striped table-bordered" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Mail</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th style="text-align: center;">
                    <input type="submit" name="submit" value="Send" class="send btn btn-primary btn-xs" disabled="" />
                </th>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td style="text-align: center;">
                    <input type="checkbox" class="checkbox" name="sendMail[]" value="<?= $certify->landlord_main_email; ?>">
                </td>   
            </tr>
        </tbody>
    </table>
</form>

Here I'm getting list of emails and ID's by checking checkbox. 在这里,我通过选中复选框获取电子邮件和ID的列表。

Controller: 控制器:

public function sendMail($certID)
    {
        # code...

        if(isset($_POST['submit'])){//to run PHP script on submit
            if(!empty($_POST['sendMail'])){
            // Loop to store and display values of individual checked checkbox.
                foreach($_POST['sendMail'] as $selected){
                    //echo $selected."</br>";
                }
            }
        }
        $result['certMails'] = $this->c->getCertificateByEmail($certID);
        $email_to = $this->input->post('sendMail[]');
        $body = $this->load->view('Emails/emailAll', '', true);
    }

Here I'm trying to get data and send it to email template view. 在这里,我试图获取数据并将其发送到电子邮件模板视图。

Model: 模型:

public function getCertificateByEmail($certID)
    {
        # code...
        $query = $this->db->query("call getCertificateByEmail($certID)");

        if ($query) {
            $data = $query->result();
            $query->next_result(); 
            $query->free_result();
            return $data;
        } else {
            return false;
        }
    }

Stored Procedure: 存储过程:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `Certificate_By_Email`(IN `certID` INT)
BEGIN
SELECT `cert_id`, `cert_user_id`, `cert_landlord_id`,
        tbl_landlord.landlord_id, tbl_landlord.landlord_main_email, 
        `cert_property_id`, tbl_property.property_code, 
        tbl_property.property_city,  tbl_property.property_cluster, 
        tbl_cluster.cluster_name, `cert_status`, `cert_number`, 
        tbl_certificates.certificate_name, `cert_issue_date`, 
        `cert_expiry_date`, `cert_duration`, `cert_updated_date`, 
        `cert_remarks`, `cert_notes` 
FROM `tbl_certificate_details` 
    LEFT OUTER JOIN tbl_landlord ON  tbl_certificate_details.cert_landlord_id = tbl_landlord.landlord_id 
    LEFT OUTER JOIN tbl_property ON tbl_certificate_details.cert_property_id = tbl_property.property_id 
    LEFT OUTER JOIN tbl_certificates ON tbl_certificate_details.certificate_id = tbl_certificates.certificate_id 
    LEFT OUTER JOIN tbl_cluster ON tbl_property.property_cluster = tbl_cluster.cluster_id 
    LEFT OUTER JOIN tbl_area ON tbl_property.property_area = tbl_area.area_name 
WHERE FIND_IN_SET(cert_id, certID);
END$$
DELIMITER ;

Any kind of help is welcome, thanks in advance. 谢谢您的任何帮助。

  1. I can't see $certID defined anywhere in sendMail() . 我在sendMail()任何地方都看不到$certID定义。 What value do you get if you print that out? 如果将其打印出来,您将获得什么价值?
  2. I'm fairly sure you just use $this->input->post('sendMail'); 我相当确定您只使用$this->input->post('sendMail'); instead of $this->input->post('sendMail[]'); 而不是$this->input->post('sendMail[]'); to get the array 得到数组
  3. Have you confirmed that your (unsafe) sql call works with custom values? 您是否已确认您的(不安全的)SQL调用可以使用自定义值? If it does, make sure you're passing the correct values 如果是这样,请确保传递正确的值

Try debugging and troubleshooting and you'll probably find what's the cause. 尝试调试和故障排除,您可能会找到原因。

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

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