Viewed   645 times

Small problem regarding scope in PHP, I can't seem to call the variable $report outside of the while loop. I have tried various things, including return. This doesn't work, the only two functions that work here are if I echo the variable $report inside the loop, or if I print it. Which I do not want to do, although it solves the problem, but I don't want random gibberish on the user's screen.

I have been looking around for the last 15 or so minutes, and I haven't seen any problems quite like this one on here.

Any help would be appreciated.

require "functions2.php";
require "members.php";
$query = "SELECT MAX(DOCid) as prevDOCid from reports";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
    $prevDOCid = $row[prevDOCid];

$thisDOCid = $prevDOCid+1;
$report = "a"."b".$thisDOCid;

echo $report;



You could try to define the variable before the loop, e.g.

$report = "";
while ($row = mysql_fetch_array($result)) {
    $report .= "a"."b".$row["prevDOCid"]+1;
echo $report;

I hope this helps you!

Edit Use .= not +=

Friday, October 21, 2022

Build an array up as you iterate with the while loop.

$result = mysql_query("SELECT * FROM `Departments`");
$results = array();
while($row = mysql_fetch_assoc($result))
   $results[] = $row;

Alternatively, if you used PDO, you could do this automatically.

Monday, October 24, 2022

The variable i that checks the condition is the one you declared in main() not the one inside the loop.

Both are different variables you are confusing them as one, the compiler doesn't get confused as easily as you were.

Inside the loop i refers to the one you declared inside the { } but outside the { } the i refers to the one declared in main()

Friday, September 23, 2022

The pipe operator creates a subshell, see BashPitfalls and BashFAQ. Solution: Don't use cat, it's useless anyway.

while read namesInFile
    echo "weeeeeeeeee ---> $postPrioity <--- 1"
done < /files.txt
echo "weeeeeeeeee ---> $postPrioity <--- 2"
Friday, December 16, 2022



$database = sqlite_open("thenew.db", 0999, $error);
if(!$database) die($error);

$query = "SELECT * FROM users";
$results = sqlite_query($database, $query);
if(!$results) die("Canot execute query");

$data = array();

while($row = sqlite_fetch_array($results)) {
  $data[] = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
echo json_encode(array("response"=>$data));


Friday, November 25, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :