0

Say I do something like this:

//get unit id
$query = "SELECT id FROM units WHERE unit_name = '".$unit."'";

$id = mysqli_query($con, $query);

$unit_id = 0;

while ($row = mysqli_fetch_array($id))
{
    $unit_id = $row['id'];
}

why is $unit_id not changed outside of the while loop?

What happens is this: I have a selection dropdown with a list of units and when on is clicked that php code is fired (along with other code in the file) and it makes a hidden input field with the id in it. I unhide the id and find that the id is not correct. What is displays, rather, is, say I click the first option, 1001, second option, 1002, third, 1003, etc. These ids do not correspond to my database at all, though the units begin at 1001 in the database. Because of all of that I assumed that my $unit_id just wasn't getting read properly and that somehow PHP didn't let one access the variable outside of a while loop in that way. I see now that assumption was premature. Thanks.

4
  • 1
    Why do you say it's not changed? Commented Nov 9, 2013 at 19:33
  • What do you mean $unit_id not changed outside of the while loop -- try echoing $unit_id after the while loop. Does it output anything? (Make sure you have enabled error reporting -- add ini_set('display_errors',1); error_reporting(E_ALL); to the top of your script) Commented Nov 9, 2013 at 19:33
  • Add echo($query) and run in MySQL. Check the result of the query. Commented Nov 9, 2013 at 19:38
  • If you put in an exit when !$id does it exit early? Commented Nov 9, 2013 at 19:39

3 Answers 3

1

Two possible explanations:

  1. Your query is failing but you have error reporting disabled (nor are you outputting/logging MySQL errors).
  2. There is no unit_name with that name.

If you don't have error reporting enabled by default, try putting:

ini_set('display_errors',1); 
error_reporting(E_ALL);

at the top of your script (in a dev enviornment, error reporting should be enabled by default, by the way). Also, you can try using:

$id = mysqli_query($con, $query) or trigger_error(mysqli_error($con));

to view any MySQL errors that may have occurred.

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

1 Comment

@AmalMurali I know. Try working on a large app that uses or die() everywhere. A nightmare to try and debug query issues.
1

Actually it changes.

Two cases for this..

  • Maybe your $row['id'] value is also 0.

[or]

  • Your query returning (0 results) and it is not entering the loop.

4 Comments

Why does the value of $row['id'] matter? Even if it is 0, an echo would output it.
OP says the value is not changed right ? So if the $row['id'] is actually 0 it will be assigned to $unit_id, so OP maybe assuming that the zero which he actually assigned is not changed.[But actually it does]
@user2690363: You should remove it from the question, add it as a new answer and mark it as 'accepted'.
Yes , you can answer your own question. This is legal on SO :)
0

What happens is this: I have a selection dropdown with a list of units and when on is clicked that php code is fired (along with other code in the file) and it makes a hidden input field with the id in it. I unhide the id and find that the id is not correct. What is displays, rather, is, say I click the first option, 1001, second option, 1002, third, 1003, etc. These ids do not correspond to my database at all, though the units begin at 1001 in the database. Because of all of that I assumed that my $unit_id just wasn't getting read properly and that somehow PHP didn't let one access the variable outside of a while loop in that way. I see now that assumption was premature. Thanks.

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.