I am using PHP's get method. What is the professional way to handle a null value in a get method. I don't want to see the sql errors on my website.
Asked
Active
Viewed 41 times
-4
-
The question is both too broad *and* unclear; what sql? and what did you try before posting this? – Funk Forty Niner Jan 20 '18 at 15:19
-
Please provide some code/examples for context. – Progrock Jan 20 '18 at 15:27
1 Answers
2
This will check if the variable is set and the variable doesn't equal an empty string - if its a number you are testing and it has to be more than 0 then swap out the "" for 0:
if (isset($_GET["myvar"]) && $_GET["myvar"] != "")
{
//do your thing
}
else
{
//if you do not wish to exit script comment this line out
exit("There has been an error.");
//use this instead
$error = "There has been an error."
}
//and echo your error out later on your page if you require it
echo $error;

garrettlynchirl
- 790
- 8
- 23
-
what they asked is a 2 part question. Did you not read their entire post? – Funk Forty Niner Jan 20 '18 at 15:19
-
-
@Funk Forty Niner, yes I did. I assumed they do not want php handing the error and they wanted to control the error message via an exit. If that is not the case I have edited and provided an alternative. It is probable that will need to either stop the script or show the user an error. – garrettlynchirl Jan 20 '18 at 15:24
-
I felt the question was too broad and unclear, which is what I posted in a comment up there. It seems that they haven't put much effort into this, IMHO. – Funk Forty Niner Jan 20 '18 at 15:28
-
Plus, seeing [their previous question](https://stackoverflow.com/q/47785909/1415724); they appear to not know how to close questions properly, nor did they comment under their post and the answer provided. So.... bit of a rabbit hole maybe? – Funk Forty Niner Jan 20 '18 at 15:31
-
@Funk Forty Niner, perhaps but I would like to think that if somebody posts a question here (and the question makes sense - errors due to learning accepted) then they are trying to solve a problem so I aim to help. – garrettlynchirl Jan 20 '18 at 15:36
-
Rightly so :-) However, you know just as I that Stack isn't a one-way street; basically a "you scratch my back and I'll scratch yours", but they appear to not like scratching other people's backs *lol!* but I'll upvote. – Funk Forty Niner Jan 20 '18 at 15:38