Hi all,

In the section of the order confirmation mail: $get_cart_info, when I include this theres is somehow a syntax error, as I keep getting this error after mail has tried to send:

Catchable fatal error: Object of class mysqli_result could not be converted to string in /home/www/ebm-fashion.dk/views/betaling_accepteret.php on line 129 (which is $get_cart_info query).

I am somehow stopping the email there. Isnt it possible to make a query inside this varaible when it is included in mail() function?

Its a pretty lame order confirmation if not even the products are in there, so I hope someone can point out what I am doing wrong!

THIS IS MY CODE:

$order_mail_comfirmation = '<div style="width:80%;">
    <p>Tak fordi du handlede hos ebm-fashion.dk.</p>
    <small>Der er registreret flg. detaljer vedrørende dit køb:</small><br /><br />
    <table>
        <tr>
            <td><small><b>Transaktions id:</b></small></td>
            <td style="padding-left:20px;"><small>'.$_SESSION['transaktions_id'].'</small></td>
        </tr>
        <tr>
            <td><small><b>Ordre id:</b></small></td>
            <td style="padding-left:20px;"><small>'.$_SESSION['order_id'].'</small></td>
        </tr>
        <tr>
            <td><small><b>Beløb hævet på kort:</b></small></td>
            <td style="padding-left:20px;"><small>'.number_format($samlet_pris,2).'</small></td>
        </tr>
        <tr>
            <td><small><b>Dato og tid:</b></small></td>
            <td style="padding-left:20px;"><small>'.$added_time.'</small></td>
        </tr>
    </table>
    <br />
    <small>
    Følgende vare(r) vil blive <b>afsendt</b> indenfor 1-2 hverdage, såfremt det ikke er en bestillingsvare.
    </small><br /><br />
<table>
'.$get_cart_info = mysqli_query($connection, "
SELECT produkt_id, navn, vare_type, model, antal, pris, str 
FROM cart_indloest WHERE user_id = '".$user_id."' AND ordre_id = '".$_SESSION['order_id']."'") or die(mysqli_error($connection));
while ($cit = mysqli_fetch_array($get_cart_info)) // $cit = cart items
    {
        // Hent billed extension fra produkter så billedet af det købte prdukt kan vises på kvitteringen:
        $get_img_info = mysqli_query($connection, "SELECT img FROM produkter WHERE id = '".$cit['produkt_id']."'");
        while ($img_ext = mysqli_fetch_array($get_img_info))
            {
                $img = $img_ext['img'];
            }

        '<tr>

          <td style="border:1px solid #666;">
              <img src="../backend/produkt_img/backend_img/'.$cit['produkt_id'].'.'.$img.'" width="60" height="90" />
          </td>

          <td style="padding-left:15px; padding-right:15px; background-color:#EEE; border:1px solid #666;">
              <small style="color:#333; font-weight:bold;"><b>• '.$cit['antal'].' par '
              .ucfirst($cit['navn']).' '
              .ucfirst($cit['vare_type']).' - Model '
              .ucfirst($cit['model']).' - Stk. pris: '
              .number_format($cit['pris'],2).' DKK. - Størrelse: '
              .$cit['str'].'</b></small>
          </td>
        </tr>';
    }
'</table>
<br />
<small>
EBM-Fashion forventer at varerne vil blive <b>leveret</b> indenfor 2-3 hverdage:
</small>
<br /><br />
<hr />
<br />
<small><b>Modtager og adresse er registreret til:</b></small>
<br />
<small> '.ucfirst($_SESSION['fornavn']).' '.ucfirst($_SESSION['efternavn']).' </small>
<br />
<small> '.ucfirst($_SESSION['gade']).' '.$_SESSION['hus_nr'].'<br />'.$_SESSION['post_nr'].' '.$_SESSION['by'].' </small>
<br /><br />
<small><b>Der er afsendt en kvittering til:</b></small><br />
<small> '. $_SESSION['email'].' </small><br /><br />
</div>';




// Send email til kunden her:
    $to         = $_SESSION['email'];
    $subject    = 'Ordrebekræftelse fra EBM-fashion';   
    $message    = $order_mail_comfirmation;

    $headers    = "Content-type: text/html; charset=utf-8rn";                   
    $headers    .= '<div style="width:500px; height:40px; padding-top:7px; background-color:#ccc; border:1px solid #666;">
    <p style="color:#666; margin-left:100px;">
    <b>Ordrebekræftelse</b>
    </p></div>';  
    $headers .= '<hr style="width:80%;" align="left"/>';     
    mail($to, $subject, $message, $headers);

Dani AI

Generated

Short diagnosis and fix (what's actually failing)
The error means you tried to use a mysqli_result object as a string. In your posted code the query/assignment is placed inside the big HTML string, so PHP evaluates the assignment and then attempts to concatenate the resulting mysqli_result into the message. That triggers the "could not be converted to string" fatal error. Do the database query and the loop first, build the HTML rows, then append that HTML into the mail body.

Concrete steps to fix

  1. Run the SELECT with mysqli_query (or prepared statement) and check for errors.
  2. Start your mail HTML in a string, open the table, then loop over the result set and append each row to the HTML using the string-concatenation operator (.=).
  3. Close the table and finish the message, then call mail() with the completed HTML message.
  4. Keep actual HTML out of the email headers — headers must contain MIME and From info only.

Small example of the correct pattern

$rows = mysqli_query($conn, "SELECT product_id,name,qty,price,size,img FROM cart WHERE user_id=$uid AND order_id=$orderId");
$html = '<table>';
while ($r = mysqli_fetch_assoc($rows)) {
  $html .= '<tr><td><img src="/img/'.$r['product_id'].'.'.$r['img'].'"></td>';
  $html .= '<td>'.htmlspecialchars($r['name']).' — '.$r['qty'].' x '.number_format($r['price'],2).'</td></tr>';
}
$html .= '</table>';
$message = '<html><body><p>Thanks …</p>'.$html.'</body></html>';
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: EBM-Fashion <no-reply@yourdomain.com>\r\n";
mail($to,$subject,$message,$headers);

Extra tips

  • Avoid a SELECT inside the loop for the image; join the products table in the main query to fetch the image in the same result set.
  • Sanitize/cast user_id and order_id (or use prepared statements) to prevent injection.
  • For debugging, echo $message in the browser before calling mail() to confirm the HTML.

Reference: — the offending line is the inline assignment of the query inside the string; already asked for that line — fixing it as above will resolve the fatal error.

Recommended Answers

All 3 Replies

How come I cant include this file here inside my mail() message?
`<?php
$get_cart_info = mysqli_query($connection, "
SELECT produkt_id, navn, vare_type, model, antal, pris, str
FROM cart_indloest WHERE user_id = '".$user_id."' AND ordre_id = '".$_SESSION['order_id']."'");
while ($cit = mysqli_fetch_array($get_cart_info)) // $cit = cart items
{
// Hent billed extension fra produkter så billedet af det købte prdukt kan vises på kvitteringen:
$get_img_info = mysqli_query($connection, "SELECT img FROM produkter WHERE id = '".$cit['produkt_id']."'");
while ($img_ext = mysqli_fetch_array($get_img_info))
{
$img = $img_ext['img'];
}
echo
'<tr>

    <td style="border:1px solid #666;">
        <img src="../backend/produkt_img/backend_img/'.$cit['produkt_id'].'.'.$img.'" width="60" height="90" />
    </td>

    <td style="padding-left:15px; padding-right:15px; background-color:#EEE; border:1px solid #666;">
    <small style="color:#333; font-weight:bold;"><b>&bull; '.$cit['antal'].' par '
    .ucfirst($cit['navn']).' '
    .ucfirst($cit['vare_type']).' - Model '
    .ucfirst($cit['model']).' - Stk. pris: '
    .number_format($cit['pris'],2).' DKK. - Størrelse: '
    .$cit['str'].'</b></small>
    </td>
    </tr>';
}

?>`

This is the order confirmation I want to email to the customers, but I get this FATAL ERROR...
Catchable fatal error: Object of class mysqli_result could not be converted to string in /home/www/xxx/views/betaling_accepteret.php on line 128

This is the mail where I want to include however number of records contained in the variable $get_cart_info.
<?php $order_mail_comfirmation = ' Tak fordi du handlede hos ebm-fashion.dk.

Der er registreret flg. detaljer vedrørende dit køb:

Transaktions id: '.$_SESSION['transaktions_id'].' Ordre id: '.$_SESSION['order_id'].' Beløb hævet på kort: '.number_format($samlet_pris,2).' Dato og tid: '.$added_time.'
Følgende vare(r) vil blive afsendt indenfor 1-2 hverdage, såfremt det ikke er en bestillingsvare.

THIS IS WHERE I WANT TO OUTPUT $GET_CART_INFO; // BUT THE EMAIL DOESNT SEND IF I INCLUDE THIS VARIABLE, AND THROWS ME THE MENTIONED FATAL ERROR. I NEED TO DO A LOOP, AS THERE MIGHT BE MORE THAN ONE ROW TO RETRIEVE, AS THE CUSTOMERS COULD BUY MORE THAN ONE ITEM.
EBM-Fashion forventer at varerne vil blive leveret indenfor 2-3 hverdage:


Modtager og adresse er registreret til:
'.ucfirst($_SESSION['fornavn']).' '.ucfirst($_SESSION['efternavn']).'
'.ucfirst($_SESSION['gade']).' '.$_SESSION['hus_nr'].'
'.$_SESSION['post_nr'].' '.$_SESSION['by'].'

Der er afsendt en kvittering til:
'. $_SESSION['email'].'

'; // Send email til kunden her: $to = $_SESSION['email']; $subject = 'Ordrebekræftelse fra EBM-fashion'; $message = $order_mail_comfirmation; $headers = "Content-type: text/html; charset=utf-8\r\n"; $headers .= ' Ordrebekræftelse '; $headers .= ''; mail($to, $subject, $message, $headers); ?>

Anyone?

NEVERMIND, this editor completely fucks up what i write!

What happended to the old one..

Gotta find a new good forum, as this is driving me crazy.

Anyone knows a good one?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.