0

If I run this query without initializing theproducts variable the code populates the <ul> properly but I get an undefined variable: products in... error. I read in the php manual that when using prepared statements they return an extra (empty) result set as a result of having called the statement itself. Could this be the problem or is it because the HTML renders first, sees the empty product variable and fires off this error because when I initialize $products = "" all is fine. Curious to know exactly what's going on.

line 67: $sql = $db->prepare("SELECT * from item WHERE user_id = '$pid'");

$sql->execute();

$products = "";

while($row = $sql->fetch())
{
  $item_id =  $row['item_id'];
  $user_id =  $row['user_id'];
  $item_name = $row['item_name'];
  $item_description = $row['item_description'];
  $date = $row['add_date'];
  $image = $row['photopath'];

  $products .= "<li><a href='item_view.php?pid=".$item_id."'><img src='$image' 
width='50' height='50'/></a> Item ID: $item_id UserID: $user_id NAME: $item_name Added on:    $date DESCRIP: $item_description</li>";

in the HTML

<ul>
line 112: <?php echo $products; ?>
</ul>
7
  • show your line number ? Commented Nov 8, 2013 at 2:22
  • Is this an include? How is your HTML related to the PHP code if it's not inline? Commented Nov 8, 2013 at 2:23
  • HTML is under php in same file. ONly include is the connect Commented Nov 8, 2013 at 2:23
  • sorry i mean undefined variable should tell you at which line it happens Commented Nov 8, 2013 at 2:26
  • I don't know where you read that you get an extra result set when using prepared statements. I suspect you misunderstood. Commented Nov 8, 2013 at 2:27

2 Answers 2

2

The reason for the error is because you use .= to concatenate to $products. This requires the variable to already have a value, so it can add to it. If it's not initialized, you get a notice about an undefined variable the first time through the loop.

It treats this undefined value as an empty string, so the concatenation is effectively the same as an initialization on this first iteration. Future iterations behave normally, because the variable has a value then. But if you have warnings enabled, you get a notice on the first iteration, complaining about the uninitialized variable. It's always best to initialize your variables, rather than depending on this automatic coercion.

Sign up to request clarification or add additional context in comments.

14 Comments

but isnt $products being initialized after first loop in the while?
@rogerthat during the first loop it tries to evaluate the right side of the assignment and fails since $products has not been defined yet
@mario you missed the last part of his question when he says that when he added $products = ""; it started working
@rogerthat It doesn't have a value until after the assignment. But it needs the value to do the assignment, because you're using .=. Look at alfasin's answer, doesn't that make it clear?
$products .= "string" is just a shorthand for $products = $products . "string". It doesn't behave any differently, they both depend on $products already having a value so it can be combined with the string.
|
2

By doing $products .= you are actually doing: $products = $products . which is concatenation of products to itself and another string. Obviously it fails when product is not defined.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.