0

I have while loop in a PHP file and outside of that, I have one variable. When I try to add this variable into while, it doesn't work.

example code:

<?

$var1 = 2999288;

while($row = mysql_fetch_array($result)){

echo $var1;

}

?>

Can anybody tell me a solution, how to echo that variable into a while? Or what I'm doing wrong?

5
  • 6
    If the variable is not echoing, then you're DB Query probably isn't returning any results. Before the loop do a: echo mysql_affected_rows($db); Commented Sep 30, 2013 at 17:49
  • Try echo "hello" instead of echo $var and see if its echoing. Commented Sep 30, 2013 at 17:49
  • 2
    I'm with @Jasper on this one. The only reason for this to not work is that your query either did not work or did not return any results. Adding error handling and checking for edge cases when writing your code would reveal this. Commented Sep 30, 2013 at 17:51
  • 1
    it seems that $result has a value false so check your sql query Commented Sep 30, 2013 at 18:04
  • Thank you guys! :D while wasn't returning nothing... :-) Commented Sep 30, 2013 at 18:06

1 Answer 1

2

Your SQL query is not returning any rows, make sure that your query is working properly before echoing results. Also please look into prepared SQL statements, they are more safe than the traditional way that you are using.

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

2 Comments

There's nothing he's posted that says he's not using prepared statements or at least not parameterizing his inputs. A real concern is that mysql_* is deprecated and he should use mysqli or pdo.
You are right, I assumed he wasn't using prepared statements since he was using mysql_*.

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.