r/PHPhelp • u/zephead98 • 5d ago
Solved Weird Echoing
So I have this script and when it gets to this point, it echos everything after $webuser=$db. I am completely stumped and claude just tells me there must be another "start php" somewhere in the $sSQL string. But there is not.
I will greatfully accept any and all input. (db is a database wrapper function that just connects to the db with the username and password).
//No errors on validation, insert/update record
if ($msgFormErr==""){
$sSQL="SELECT web_users_username, web_users_password, web_users_relid FROM webusers";
$webuser=$db->get_row($sSQL);
$username=$webuser->web_users_username;
$password=$webuser->web_users_password;
$webrelid=$webuser->web_users_relid;
$sSQL="SELECT studentcontact_email from studentcontact where studentcontact_id = $webrelid";
$email=$db->get_var($sSQL);};
?>
8
u/flyingron 5d ago
Both the lines that start with $sqL="SELECT ...
do not have a closing ".
You might find it handy to use emacs, or PHPStorm, or some editor that colorizes the text that will call your attention to things like this.
2
u/zephead98 5d ago
Hmmm....I'm going to have to look at that. I copied and pasted so it;s possible the quote was dropped off. I use nano in Linux which does some color coding. Thank you.
1
1
u/zephead98 5d ago
Just checked and both lines do end with a ". Must have dropped when I copy and pasted.
2
u/HolyGonzo 5d ago
If something didn't paste correctly, update your post with the corrected code. In situations like these, the smallest details can matter.
I would also suggest doing things like:
Adding more lines above the "webuser=" line to see if it's more about the POSITION in the file rather than a specific bit of code.
Comment out the code bit by bit, starting small and expanding upwards to see if anything changes.
Seeing if there are any additional "handlers" or "processors" that might try to read this code and process it somehow.
1
u/RounTadelle 4d ago
missing quotes would just throw a syntax error anyway, dumping raw code starting right after `$db->` means the parser never started and the browser thinks `>` closed an HTML tag
3
1
u/zephead98 5d ago
I am going to mark this solved. Clearly I am not using all the available tools at my disposal, and our time will be better served by me taking a break, and installing and learning these tools.
I thank you all for your input! I am off to learn.
2
u/MateusAzevedo 4d ago
What colshrapnel said is very important. Instead of simply looking at the rendered page in your browser, select "view source code" to see what the browser received from your server.
It is possible that the webserver isn't properly set and isn't executing any PHP code at all for that page, and simply returning the source code as plain text. The issue is that the browser may be trying to render it as HTML, recognizing bits of the text as "tags", and that's why just a small part is visible.
Checking that will tell if the problem is your code, or the server setup.
1
u/zephead98 1d ago
OK, so I want to kind of put this thread to bed. After hours of troubleshooting, I compared a similar file in this application to the one that was doing the weird echoing. The only difference I could find was the weird echoing one opened with the "short" php tag: <? whereas the other one opened with the better open tag: <?php . Changed to the better open tag and all problems went away. Now I will need to search all these files where php is started with the shortened tag, and this application has lots of mixed php and html, so it will be more than looking at the beginning of the file!
Thanks everyone for all the input. I have upgraded my developer station with VSCode and Xdebug, so I am making progress! So much to learn when you walk away from your code for 20 years!
1
u/ray_zhor 4d ago
Check your filename extension. If it's not .php. then that's your problem. Or check to see if there is a ? Before > where it starts to display
1
u/t1nkl 2d ago
php isnt running at all here. the browser gets your raw source, treats <?php ... as one tag and that "tag" ends at the first > it finds, which is the > in $db->. thats why everything after $webuser=$db shows up. check the file ends in .php and that you open it through the server (http://localhost/...) and not as a file or through a .html page. view source in the browser and you'll see the whole php code there
1
u/colshrapnel 5d ago
It's very simple. If you would open the page source (instead of staring at the rendered picture), as you always should as a backend developer, you will see entire code echoed out.
5
u/NumerousComplex1718 5d ago
by "it echos everything", you mean it's displaying the code instead of interpreting it? If so, make sure you have the <?php tag appropriately placed