简体   繁体   中英

How do I get the result of a “SELECT” query and store it in a variable

I have a query in my code which looks like this

$edi_transaction_id = mysqli_query($conn, "SELECT a.edi_transaction_id FROM edi_transaction_detail a JOIN reference_number b JOIN edi_transaction c WHERE a.asn_number = '$asn_number' AND a.edi_transaction_id = c.edi_transaction_id LIMIT 1");

I would like to get the value of that query and store it in $edi_transaction_id variable. I already thought this would run because it is working correctly when done on the mysql database directly but when I integrate it in my code this is the error

<b>Catchable fatal error</b>:  Object of class mysqli_result could not be converted to string in <b>/home/ationgzon/WebService/edi_864_824_files.php</b> on line <b>53</b><br />

This is the line number 53 in edi_864_824_files.php

mysqli_query($conn, "INSERT INTO `edi_864_824`(`edi_transaction_id`, `trading_partner`, `trans_date`, `issue`, `reference_number_id`) VALUES ($edi_transaction_id, '$trading_partner', '$trans_date', '$message', $reference_number_id)");

How do I do it so that I can insert it in my database.

Try this way

    $edi_transaction_id = mysqli_query($conn, "SELECT a.edi_transaction_id FROM edi_transaction_detail a JOIN reference_number b JOIN edi_transaction c WHERE a.asn_number = '$asn_number' AND a.edi_transaction_id = c.edi_transaction_id LIMIT 1");
    while($row = $edi_transaction_id->fetch_array(MYSQLI_NUM)){
    $edit_t_detail = $row[0];
    }
mysqli_query($conn, "INSERT INTO `edi_864_824`(`$edit_t_detail`, `trading_partner`, `trans_date`, `issue`, `reference_number_id`) VALUES ($edi_transaction_id, '$trading_partner', '$trans_date', '$message', $reference_number_id)");

Here, $edi_transaction_id is the type 'mysqli_result object' so you cannot convert it to string

尝试改用mysqli_fetch_array,请参见此处的完整文档: http ://php.net/manual/zh/mysqli-result.fetch-array.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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