如何获取“SELECT”查询的结果并将其存储在变量

时间:2016-07-01 01:39:17

标签: php mysql

我的代码中有一个查询,看起来像这样

$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");

我想获取该查询的值并将其存储在$ edi_transaction_id变量中。我已经认为这会运行,因为它直接在mysql数据库上完成时工作正常但是当我将它集成到我的代码中时这是错误

<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 />

这是edi_864_824_files.php

中的第53行
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)");

我该怎么做才能将其插入我的数据库。

2 个答案:

答案 0 :(得分:1)

试试这种方式

    $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)");

此处,$edi_transaction_id是'mysqli_result对象'类型,因此您无法将其转换为字符串

答案 1 :(得分:-1)

尝试使用mysqli_fetch_array,可在此处获取完整文档:http://php.net/manual/en/mysqli-result.fetch-array.php