-4

I have a PHP loop.

Inside a loop I will have a Select statement.

I get error

Fatal error: Uncaught Error: Cannot pass parameter 2 by reference

I don't know what I am doing wrong.

This is my statement

<?php
$query = "SELECT customer.id AS customerid,customer.doc, 
                 order.id AS orderid ,order.doc FROM customer
            Inner Join order ON customer.doc = order.doc
          Where customer.doc= ?";
if ($stmt = $mysqli->prepare($query)) {

    $stmt->bind_param("i", 'DOC48599');

    /* execute query */
    $stmt->execute();

    /* store result */
    $stmt->store_result();

    printf("Number of rows: %d.\n", $stmt->num_rows);

    /* close statement */
    $stmt->close();
}

/* close connection */
$mysqli->close();

?>

And my second question is what is the best practice to have Select statement inside Loop.

So for each loop, it's going through; it will get an ID and do select statement binding the ID in the select statement.

Thanks

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    `i` stand for integer and `DOC48599` there's no way that can be an int. `$code = 'DOC48599'`; `$stmt->bind_param("s", $code');` should solve this – Masivuye Cokile Mar 20 '19 at 10:20
  • 1
    That’s not what is causing the error here though, but the fact that you need to pass _variables_ for the parameter values, not scalar values. – 04FS Mar 20 '19 at 10:22
  • 1
    _“And my second question is what is the best practice to have Select statement inside Loop.”_ - best practice is to **not** have that, but to see how you can join whatever data you need in one single statement, if at all possible. – 04FS Mar 20 '19 at 10:23

1 Answers1

1

The error is in this line:

$stmt->bind_param("i", 'DOC48599');

You cannot pass values directly the string 'DOC48599' as a parameter, you need to store it in a variable and then pass the variable to the bind_param method.

You also have to pay attention to the type of the parameter passed: if your parameter is a string, you need to use "s" instead of "i". Take a look here

$myParameter = 'DOC48599';
$stmt->bind_param("s", $myParameter);
Script47
  • 14,230
  • 4
  • 45
  • 66
kiks73
  • 3,718
  • 3
  • 25
  • 52
  • I have got this working. thanks, everyone. Just have another question. Because this select statement is in the loop. It works for the first time. But on the second iteration I get error 'Warning: mysqli::prepare(): Couldn't fetch mysqli in '. How can I fixes this – James Hickling Mar 20 '19 at 10:37