r/PHPhelp • • 2d 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);};

?>

3 Upvotes

15 comments sorted by

6

u/NumerousComplex1718 2d 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

1

u/zephead98 2d ago

That is correct. In the webpage it displays the code. So weird! Just after that webuser=$db

One open php tab at the top of the script and one close php at the end of the script.
I am beginning to think it might be related to the "db function".

Thank you.

1

u/colshrapnel 1d ago

It's related to the fact this code is not executed by PHP

8

u/flyingron 2d 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 2d 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

u/Alexander-Wright 1d ago

VSCode is another free option with PHP plugins and support for phpdebug.

1

u/zephead98 2d ago

Just checked and both lines do end with a ". Must have dropped when I copy and pasted.

2

u/HolyGonzo 1d 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:

  1. 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.
  2. Comment out the code bit by bit, starting small and expanding upwards to see if anything changes.
  3. Seeing if there are any additional "handlers" or "processors" that might try to read this code and process it somehow.

1

u/RounTadelle 1d 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

u/ohnomybutt 2d ago

use a debugger.

1

u/zephead98 2d ago

Thanks. I will def give that a whirl. Nice username!

1

u/zephead98 1d 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 1d 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/ray_zhor 1d 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/colshrapnel 1d 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.