0

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.

vip_noob
  • 45
  • 1
  • 10
  • 4
    `mysqli_query()` returns a mysqli_result object, not a string – Tristan Jul 01 '16 at 01:44
  • 3
    Why is it so hard to [read the manual](http://php.net/manual/en/mysqli.query.php)? It's all written there with examples! – Jeff Jul 01 '16 at 01:44
  • See: http://stackoverflow.com/questions/2668624/how-to-display-mysql-select-statement-results-in-php – FrozenFire Jul 01 '16 at 01:45
  • If you only need `edi_transaction_id` for the insert then, check out [this question/answer](http://stackoverflow.com/questions/5391344/insert-with-select) It shows how to `INSERT INTO` using the results of a `SELECT` –  Jul 01 '16 at 02:14

2 Answers2

1

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

-1

Try to use mysqli_fetch_array instead, full documentation are available here : http://php.net/manual/en/mysqli-result.fetch-array.php

P.Coder
  • 94
  • 9
  • 1
    And when you read the documentation, you'll see you need a [result object](http://php.net/manual/en/class.mysqli-result.php). As it stands, your answer doesn't fix the problem. –  Jul 01 '16 at 02:00